1

In my app, I am trying to manipulate an EditText element, but every time I try to do so, it throws a nullPointerException at me. I don't know why its doing this because it only seems to happen to this one EditText, docTitle, not for any of the others I have.

Here is the part of the java class that throws the exception:

 public class MainActivity extends FragmentActivity implements RichText.EditTextImeBackListener, OnTouchListener, OnClickListener, TextWatcher{
     private String[] drawerListViewItems1;
     private String[] drawerListViewItems2;
     private ArrayList<Editable> undoStrings;

     private int undoIndex;

     //this is the EditText that is giving me trouble, it is defined down in onCreate
     public EditText docTitle;

     private int styleStart;

     private ListView drawerListView1;
     private ListView drawerListView2;

     private RelativeLayout formatBar;

     private DrawerLayout drawerLayout;
     private ActionBarDrawerToggle actionBarDrawerToggle;
     private RichText richText;
     private SaveFragment saveFragment;

     private ToggleButton boldButton;
     private ToggleButton emButton;
     private ToggleButton uButton;
     private ToggleButton strikeButton;

     private boolean keyboardShown;
     private boolean alreadyShown;
     private boolean isMainContent;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         //finds the left drawer
         drawerListView1 = (ListView) findViewById(R.id.left_drawer);
         //finds right drawer
         drawerListView2 = (ListView)findViewById(R.id.right_drawer);

         //loads the first drawer
         loadDrawer1();

         //loads the second drawer
         loadDrawer2();

         //finds the drawer layout
         drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

         //assign ActionBarDrawerToggle
         actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, 
                 R.string.drawer_open, R.string.drawer_close);

         //sets the drawer listener
         drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
             @Override
             public void onDrawerOpened(View drawerLayout){
                 //hides format bar when a drawer is opened
                 if(keyboardShown){
                     hideFormatBarAnim();
                 }
             }

             @Override
             public void onDrawerClosed(View drawerLayout){
                 //shows format bar when closed
                 showFormatBar();
             }
         });

         //enable and show "up" arrow
         getActionBar().setDisplayHomeAsUpEnabled(true); 

         // just styling option for the drawer layout
         drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

         //item click listener for drawer items
         drawerListView1.setOnItemClickListener(new DrawerItemClickListener());
         drawerListView2.setOnItemClickListener(new DrawerItemClickListener());

         isMainContent= true;
         alreadyShown=false;
         keyboardShown=false;

         undoIndex=0;

         undoStrings = new ArrayList<Editable>();

         //assigns formatBar to its XML layout 
         formatBar = (RelativeLayout)findViewById(R.id.formatBar);

         //assigns docTitle to its XML layout
         docTitle = (EditText)findViewById(R.id.docTitle);
         //This line here throws the error
         docTitle.setText("Hello");

         //defines save fragment
         saveFragment = new SaveFragment();

         //assigns richText to its XML layout and adds a TextChangeListener
         richText= (RichText)findViewById(R.id.edit_text);
         richText.addTextChangedListener(this);

         //assigns boldButton to its XML layout and adds an OnClickListener
         boldButton=(ToggleButton)findViewById(R.id.bold);
         boldButton.setOnClickListener(this);

         //assigns emButton to its XML layout and adds an OnClickListener
         emButton=(ToggleButton)findViewById(R.id.ital);
         emButton.setOnClickListener(this);

         //assigns uButton to its XML layout and adds an OnClickListener
         uButton=(ToggleButton)findViewById(R.id.underline);
         uButton.setOnClickListener(this);

         //assigns strikeButton to its XML layout and adds an onClickListener
         strikeButton=(ToggleButton)findViewById(R.id.strike);
         strikeButton.setOnClickListener(this);

         //initiates sofKeyboardHook
         softKeyboardHook();

         //creates the default saving directory
         Doc.makeDefaultDir();
         lookForFiles();
     }

And here is the XML layout that goes with it:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:background="#1E772F"
    android:layout_marginTop="350dp">

        <EditText
            android:id="@+id/docTitle"
            android:text="DefaultDocName"
            android:paddingTop="20dp"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"/>

        <Button 
            android:id="@+id/save"
            android:layout_height="wrap_content"
            android:layout_width="150dp"
            android:text="Save"
            android:layout_below="@+id/docTitle"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:onClick="onSaveDoneClick"/>
        <Button
            android:id="@+id/cancel"
            android:layout_height="wrap_content"
            android:layout_width="150dp"
            android:text="Cancel"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/docTitle"
            android:layout_toRightOf="@+id/save"
            android:onClick="cancelSave"/>


    </RelativeLayout>
</ScrollView>

Heres the log for the error it throws:

03-19 09:48:50.246  12996-12996/com.noahl98.jotmobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.noahl98.jotmobile, PID: 12996
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.noahl98.jotmobile/com.noahl98.jotmobile.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
            at android.app.ActivityThread.access$700(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1411)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5083)
            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:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.noahl98.jotmobile.MainActivity.onCreate(MainActivity.java:133)
            at android.app.Activity.performCreate(Activity.java:5260)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
            at android.app.ActivityThread.access$700(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1411)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5083)
            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:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

Thanks in advance for your help.

3
  • 1
    Which is the line 133 of your MainActivity file? Commented Mar 19, 2014 at 14:33
  • did you check setContentView(R.layout.activity_main); code? Commented Mar 19, 2014 at 14:40
  • line 133 is docTitle.setText("Hello") Commented Mar 19, 2014 at 15:11

2 Answers 2

2

The layout you are inflating is called 'activity_main.xml' and it seems to contain a number of drawers and other things that are not in the layout file you attached. Are you sure that you are not confusing your layout files.

basically it appears like you don't have 'docTitle' in the 'activity_main.xml' file which is why

//assigns docTitle to its XML layout
docTitle = (EditText)findViewById(R.id.docTitle);
//This line here throws the error
docTitle.setText("Hello");

gives you 'docTitle = null'

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, my problem was that the XML file that has docTitle is linked to a different class, not MainActivity, so it was throwing the error because of that. Thanks again.
0

The problem is that the layout that you are loading in your onCreate function is not the one which has the editText,

you can check:

setContentView(R.layout.activity_main);

is this activity_main layout the one that you have posted?

Because later in your onCreate method you are using drawerListView1 which is not in the XML you have pasted, and that one is not null.

Please ensure: the views you're using should been in the layout that you are loading

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.