• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to resolve this null pointer exception?

I am getting null pointer exception in following code. I am not understanding where is the problem in this code. What modifications should I have to make in this code to resolve this issue?

MainActivity.java

Code:
 import android.app.Activity;
        import android.content.res.Resources;
        import android.os.Bundle;
        import android.support.v7.widget.CardView;
        import android.support.v7.widget.LinearLayoutManager;
        import android.support.v7.widget.RecyclerView;
 import android.text.Html;
 import android.text.Spanned;
 import android.view.View;
        import android.view.ViewGroup;
        import android.widget.ImageView;
        import android.widget.TextView;

        import java.util.ArrayList;
        import java.util.List;

public class MainActivity extends Activity {
    private List<Notes> chap;
    private RecyclerView rv;
    Resources res;
    CharSequence[] chapTitles;
    Spanned[] chapNotes;
    String[] noteArray;
    int count;
    int count1;

    [USER=1021285]@override[/USER]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv=(RecyclerView)findViewById(R.id.rv);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);
        initializeData();
        initializeAdapter();
    }
    private void initializeData(){
        res = getResources();
        chap = new ArrayList<>();
        chapTitles = res.getTextArray(R.array.Introductiontitles);
        noteArray = res.getStringArray(R.array.Introductionnotes);
        count = noteArray.length;
        for (int i = 0; i<count; i++) {
            chapNotes = Html.fromHtml(noteArray);
        }
        count1 = chapTitles.length;
        for (int j = 0; j<count1; j++) {
            chap.add(new Notes(chapTitles[j], chapNotes[j]));
        }
    }
    private void initializeAdapter(){
        RVAdapter adapter = new RVAdapter(chap);
        rv.setAdapter(adapter);
    }
}

Logcat:

Code:
04-17 14:01:16.185 21069-21069/com.ilearn.itemdetail E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.ilearn.itemdetail, PID: 21069
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ilearn.itemdetail/com.ilearn.itemdetail.MainActivity}: java.lang.NullPointerException
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:141)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:136)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5113)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        Caused by: java.lang.NullPointerException
                                                                           at com.ilearn.itemdetail.MainActivity.initializeData(MainActivity.java:48)
                                                                           at com.ilearn.itemdetail.MainActivity.onCreate(MainActivity.java:38)
                                                                           at android.app.Activity.performCreate(Activity.java:5248)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:141)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:136)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5113)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                           at dalvik.system.NativeStart.main(Native Method)
 
Last edited by a moderator:
The reason I say that it doesn't match up is because your stack trace says that the initializeData() method was called at line 38, which in your code, is

Code:
}

So that doesn't make sense. Please post the correct code, with line numbers that match the stack trace, then we might have a chance at giving you an answer.
 
Yaa u r right, in code which I do have. All the code is as it is posted,just that while posting I missed including pkg name, hence mismatching of code and logcat
At 36 it's
initializedata();

At 48 it's
chapNotes = Html.fromHtml(noteArray);
 
46 count = noteArray.length;
47 for (int i = 0; i<count; i++) {
48 chapNotes = Html.fromHtml(noteArray);
}
This is code at lines 46, 47, 48. In this no whole array is passed to fromHtml but just
one array element of string-array named noteArray.
 
You declare noteArray as an Array.

Code:
String[] noteArray;

To confirm it doesn't compile, I pasted the whole lot into Android Studio. As expected, I get a compile problem on this line

Code:
chapNotes = Html.fromHtml(noteArray);

Because fromHtml() doesn't take an array object as a parameter. It takes a String.
 
Back
Top Bottom