user2733182
user2733182

Reputation: 25

nullpointerexception in a for and if statement

I want to find 'elements in an array wjoined' which are not included in an array cjoined. I cannot find what is wrong. Please help me. Following is my code. LogCat says 'Caused by:java.lang.NullPointerException'.

for (int x = 0; x < wjoined.length; x++)
{
    int count1 = 0;
    for (int y = 0; y < cjoined.length; y++)
    {
        if (wjoined[x].equals(cjoined[y]) )
        {
            count1++;
        }
    }
    if (count1 == 0){
        String sql = "INSERT INTO ErrorCausingFactor (errorcausingelement)"+"VALUE('"+wjoined[x] +"')";
        db.execSQL(sql);
    }
}

This is my logcat.

08-30 13:34:02.618: W/dalvikvm(1150): threadid=1: thread exiting with uncaught exception (group=0x40014760) 08-30 13:34:02.628: E/AndroidRuntime(1150): FATAL EXCEPTION: main 08-30 13:34:02.628: E/AndroidRuntime(1150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demo.testdemo/com.demo.testdemo.NextActivity}: java.lang.NullPointerException 08-30 13:34:02.628: E/AndroidRuntime(1150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748) 08-30 13:34:02.628: E/AndroidRuntime(1150): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764) 08-30 13:34:02.628: E/AndroidRuntime(1150): at android.app.ActivityThread.access$1500(ActivityThread.java:122) 08-30 13:34:02.628: E/AndroidRuntime(1150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002) 08-30 13:34:02.628: E/AndroidRuntime(1150): at android.os.Handler.dispatchMessage(Handler.java:99) 08-30 13:34:02.628: E/AndroidRuntime(1150): at android.os.Looper.loop(Looper.java:132) 08-30 13:34:02.628: E/AndroidRuntime(1150): at android.app.ActivityThread.main(ActivityThread.java:4025) 08-30 13:34:02.628: E/AndroidRuntime(1150): at java.lang.reflect.Method.invokeNative(Native Method) 08-30 13:34:02.628: E/AndroidRuntime(1150): at java.lang.reflect.Method.invoke(Method.java:491) 08-30 13:34:02.628: E/AndroidRuntime(1150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 08-30 13:34:02.628: E/AndroidRuntime(1150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 08-30 13:34:02.628: E/AndroidRuntime(1150): at dalvik.system.NativeStart.main(Native Method) 08-30 13:34:02.628: E/AndroidRuntime(1150): Caused by: java.lang.NullPointerException 08-30 13:34:02.628: E/AndroidRuntime(1150): at com.demo.testdemo.NextActivity.onCreate(NextActivity.java:157) 08-30 13:34:02.628: E/AndroidRuntime(1150): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 08-30 13:34:02.628: E/AndroidRuntime(1150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712) 08-30 13:34:02.628: E/AndroidRuntime(1150): ... 11 more

Upvotes: 0

Views: 128

Answers (3)

GerritCap
GerritCap

Reputation: 1626

Your locat should give you more details than just the exception, there will also be a stacktrace that should give you an indication on which line the NPE occured.

In your code I can see 3 possible locations to have an NPE being thrown:

  1. In case wJoined is null then you will get an NPE thrown at the line number of your first for statement
  2. In case cJoined is null then you will get an NPE thrown at the line number of your second for loop
  3. if your database object db is null then you will get an NPE where you execute the insert statement.

Just inspect your stacktrace:

Caused by: java.lang.NullPointerException 08-30 13:34:02.628: E/AndroidRuntime(1150): at com.demo.testdemo.NextActivity.onCreate(NextActivity.java:157)

EDIT: 4. is wJoined[x] is null this also raises an NPE as indicated by Roddy the frozen peas :-)

So it is on line 157 where the error occurs

Upvotes: 1

Joetjah
Joetjah

Reputation: 6132

Some of the values are null. Try this:

if (wjoined != null)
{
    for (int x = 0; x < wjoined.length; x++)
    {
        int count1 = 0;
        if (cjoined != null && wjoined[x] != null)
        {
            for (int y = 0; y < cjoined.length; y++)
            {
                if (cjoined[y] != null)
                {
                    if (wjoined[x].equals(cjoined[y]) )
                    {
                        count1++;
                    }
                } else {
                    System.out.println("cjoined[y]" is null at: " + y);
                }
            }
            if (count1 == 0){
                String sql = "INSERT INTO ErrorCausingFactor (errorcausingelement)"+"VALUE('"+wjoined[x] +"')";
                if (db != null)
                    db.execSQL(sql);
                else
                    System.out.println("db is null!");
            }
        } else {
            System.out.println("cjoined or wjoined[x] at x: " + x + " is null!");
        }
    }
} else {
    System.out.println("wjoined is null!");
}

Upvotes: 0

Ivo
Ivo

Reputation: 23257

Whenever you see an Exception in the log it states the exact line where the error occurs. You should look into that. Not knowing the line i suspect either wjoined, cjoined or db is null

EDIT: seeing your log the error is on line 157 of NextActivity.java. See what that is and you probably know what's wrong

Upvotes: 2

Related Questions