Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java errors- int cannot be dereferenced

Status
Not open for further replies.

dmcclus

Technical User
Sep 10, 2008
2
0
0
CA
I have been tasked with changing a java file that I had to get from a decompiler. I am not so familiar and get errors compiling as follows:
C:\Program Files\IBM\SQLLIB\java\jdk\bin>javac C:\jarsSrc\dbms\Main.java
C:\jarsSrc\dbms\Main.java:258: int cannot be dereferenced
for(i = Integer.valueOf(0); i.intValue() < lines.length;)
^
C:\jarsSrc\dbms\Main.java:260: int cannot be dereferenced
if(lines[i.intValue()].equals((new StringBuilder()).append("NAME: ")
.append(apiname).toString()))
^
C:\jarsSrc\dbms\Main.java:262: int cannot be dereferenced
j = Integer.valueOf(i.intValue() + 1);
^
C:\jarsSrc\dbms\Main.java:262: operator + cannot be applied to <any>,int
j = Integer.valueOf(i.intValue() + 1);
^
C:\jarsSrc\dbms\Main.java:265: int cannot be dereferenced
Integer integer1 = i = Integer.valueOf(i.intValue() + 1);
^
C:\jarsSrc\dbms\Main.java:265: operator + cannot be applied to <any>,int
Integer integer1 = i = Integer.valueOf(i.intValue() + 1);
^
C:\jarsSrc\dbms\Main.java:269: int cannot be dereferenced
System.out.println((new StringBuilder()).append("Found API base url for
").append(apiname).append(": Using ").append(lines[j.intValue()]).toString());

^
C:\jarsSrc\dbms\Main.java:270: int cannot be dereferenced
return lines[j.intValue()];
^
8 errors


HERE is the section of code returning the errors:
public static String get_api_url(String apiname)
{

int i ;
int j ;
String lines[];
String html = getHTML(" BEFORE POSTING");
html = html.replace("URL: ", "");
lines = html.split("\n");
for(i = Integer.valueOf(0); i < lines.length;)
{
if(lines[i.intValue()].equals((new StringBuilder()).append("NAME: ").append(apiname).toString()))
{
j = Integer.valueOf(i.intValue() + 1);
}
Integer integer = i;
Integer integer1 = i = Integer.valueOf(i.intValue() + 1);
// integer;
}

System.out.println((new StringBuilder()).append("Found API base url for ").append(apiname).append(": Using ").append(lines[j.intValue()]).toString());
return lines[j.intValue()];
Exception e;

System.err.println("Can't find this API, returning the default.");
return " BEFORE POSTING=";
}
}
 
That code is a nightmare, but the function itself looks pretty simple, maybe you should consider rewrinting it from scratch instead of decompiling.

Cheers,
Dian
 
Hiya,

Has that code definately not been hand modified after decompilation? The reason I ask is that there is a mix of primitive integers and object Integers. As an example:

Code:
int i ;
...
for(i = Integer.valueOf(0); i < lines.length;)

You are defining 'i' as a primitive 'int', then trying to assign an 'java.lang.Integer' to it. You probably just want the following:


Code:
int i ;
...
for(i = 0; i < lines.length;)

I agree with Dian. You should consider re-writing this function for clarity, but I expect the rest of the code is the same anyway.

Cheers,
Scott
 
Don't you just love the first one?

for(i = Integer.valueOf(0); i.intValue() < lines.length;)

It doesn't even increment i within the loop!
Once again, concise and apposite advice from Scott...

Busted.
 
Thanks for the advice everyone.
I did rewrite that section of the code (this was only one section/funtion of the whole piece)

here is what I rewrote and it now compiles. Have to wait to see if it does what it is supposed to along with the other code.

public static String get_api_url(String apiname)
{

Integer i = 0;
Integer j = 0;

try {

String html = getHTML(" html = html.replace("URL: ", "");
String [] lines = html.split("\n");

for(i = 0; i < lines.length; i++)


{
if(lines.equals((new StringBuilder()).append("NAME: ").append(apiname).toString()))
{
j = i + 1;
}

}

System.out.println((new StringBuilder()).append("Found API base url for ").append(apiname).append(": Using ").append(lines[j]).toString());
return lines[j];


} catch (Exception e){

System.err.println("Can't find this API, returning the default.");
return " }
}
 
Good decision. Just some points

Code:
public static String get_api_url(String apiname)
    {
      int i = 0;
      try {
        String html = getHTML("[URL unfurl="true"]http://REMOVED");[/URL]
        html = html.replace("URL: ", "");
        String [] lines = html.split("\n");
String strToSearch = new StringBuilder().append("NAME: ").append(apiname).toString();

        for(i = 0; i < lines.length; i++) {
            if(lines[i].equals(strToSearch);            
                break;
        }

        System.out.println((new StringBuilder()).append("Found API base url for ").append(apiname).append(": Using ").append(lines[i+1]).toString());
        return lines[i+1];


    } catch (Exception e){

        System.err.println("Can't find this API, returning the default.");
        return "[URL unfurl="true"]http://REMOVED?byInternetAddr=";[/URL]
        }
    }

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top