John BG
John BG

Reputation: 366

Android ArrayList Strange Null Pointer Exception

I have problem with this code:

 for(int i2=0;i2<ftdata.size();i2++){
        System.out.println("FTDATASIZE: " +ftdata.get(i2)); // here pass and shows null
        String test=ftdata.get(i2); // here gets Null Pointer Exception ????
        if(test.equals("null")){

        }
    }

System.out shows in LogCat "FTDATASIZE: null" but I get error with this line "String test=ftdata.get(i2);" I dont get it? why I get Null Pointer Exception?

    04-07 05:42:36.309: I/System.out(12983): STDATASIZE: 1
    04-07 05:42:36.309: I/System.out(12983): FTDATASIZE: 1
    04-07 05:42:36.309: I/System.out(12983): FTDATASIZE: null
    04-07 05:42:36.309: W/dalvikvm(12983): threadid=10: thread exiting with uncaught exception (group=0x40166560)
    04-07 05:42:36.309: E/AndroidRuntime(12983): FATAL EXCEPTION: Thread-11
    04-07 05:42:36.309: E/AndroidRuntime(12983): java.lang.NullPointerException
    04-07 05:42:36.309: E/AndroidRuntime(12983):    at soft.ProDb.distanceRunsMonthly(ProDb.java:22718)
    04-07 05:42:36.309: E/AndroidRuntime(12983):    at soft.ProDb.runsMonthly(ProDb.java:22593)
    04-07 05:42:36.309: E/AndroidRuntime(12983):    at soft.SHtmlM.exportALLHtml(StringaHtmlM.java:111)
    04-07 05:42:36.309: E/AndroidRuntime(12983):    at soft.ProDb$1.run(ProDb.java:12714)
    04-07 05:42:36.309: E/AndroidRuntime(12983):    at java.lang.Thread.run(Thread.java:1019)

OK Fixed

this works for me

 if(ftdata.size()==1&& ftdata.get(0)==null){
        ftdata=stdata;
    }

Just wanted to test if ftdata.get(0) is null or not

Upvotes: 0

Views: 1206

Answers (2)

Masoud Kardani
Masoud Kardani

Reputation: 91

try this:

for(int i2=0;i2<ftdata.size();i2++){
        System.out.println("FTDATASIZE: " +ftdata.get(i2));
        String test; 
        if(ftdata.get(i2)!=null){
            test = ftdata.get(i2);
        }
    }

Upvotes: 1

Karakuri
Karakuri

Reputation: 38605

The null pointer is on the line if(test.equals("null")){ because test is null and you are trying to call .equals() on it.

If you are trying to check that the String is null, use if (test == null) instead

If you are trying to check that the String has the value "null", use if ("null".equals(test)) instead

Upvotes: 4

Related Questions