简体   繁体   中英

getting null pointer exception

I am getting a NullPointerException when I try to access text view which is defined in view class. I am accessing it from setting class. A small part of my code is:

view class

public class view1 extends menu {
    public static TextView text1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);
        text1=(TextView)findViewById(R.id.textfile1);
        text1.setText("product");
    }

    public void small(String mytext) { // this is my method which I want to access
        text1.setText(mytext);
    }
}

setting class

public class Setting extends Activity {


    private Spinner spinner1;
      private Button apply;
      TextView small1;
      private view1 view11;




      @Override


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        //setContentView(R.layout.view);

        addItemsOnSpinner1();
        addListenerOnSpinnerItemSelection();



}

    public void addItemsOnSpinner1() {

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        List<String> list = new ArrayList<String>();
        list.add("Small");
        list.add("Medium");
        list.add("Large");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(dataAdapter);
      }

    public void addListenerOnSpinnerItemSelection() {
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        apply = (Button) findViewById(R.id.apply);
        spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());


        apply.setOnClickListener(new OnClickListener() {

              @Override
              public void onClick(View v) {

                  String mytext = "Something else";
                  view11.small(mytext);





                  }

Stack trace

01-17 00:11:02.064: E/AndroidRuntime(4191): FATAL EXCEPTION: main
01-17 00:11:02.064: E/AndroidRuntime(4191): java.lang.NullPointerException
01-17 00:11:02.064: E/AndroidRuntime(4191):     at com.ramanrayat.notelet.Setting$1.onClick(Setting.java:106)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at android.view.View.performClick(View.java:4240)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at android.view.View$PerformClick.run(View.java:17721)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at android.os.Handler.handleCallback(Handler.java:730)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at android.os.Looper.loop(Looper.java:137)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at java.lang.reflect.Method.invokeNative(Native Method)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at java.lang.reflect.Method.invoke(Method.java:525)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-17 00:11:02.064: E/AndroidRuntime(4191):     at dalvik.system.NativeStart.main(Native Method)

setting.xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:text="Setting"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="57dp"
        android:gravity="center"
        android:text="Font Size"
        android:textSize="30dp" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp" />

    <Button
        android:id="@+id/apply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/textView2"
        android:layout_below="@+id/spinner1"
        android:layout_marginTop="58dp"
        android:text="Apply" />

</RelativeLayout>

Chnage

 if(String.valueOf(spinner1.getSelectedItem())=="Small")

TO

if(String.valueOf(spinner1.getSelectedItem()).equals("Small"))

Use .equals or .equalsIgnoreCase to compare strings

Instead of making textview static you should use intents to pass values between activities.

Intent intent = new Intent(ActivityName.this,Settings.class);
intent.putExtra("key",text1.getText().toString());
startActivity(intent);

Then

String value = getIntent().getStringExtra("key");

Change

  public static TextView text1; 

to

   public TextView text1; 

Also follow java naming conventions

Replace

public static TextView text1;

with

TextView text1;

text1 can not be static.

Next time, please click on the file name in the error log and indicate which line number in your code listing is the line that the error actually points to.

Learn to start all your class names with capital letters (athough, this is not what's causing the problem). And while I'm at it, please stop using numbers in class names and in variables, especially the number 1, which can be ambiguously read as an "l" in some fonts.

Look for your LogCat, it will told you where is NPE.
The other question is : don't use equals to compare String instead of ==

== is compare two object's address and equals is compare their value.

 if(String.valueOf(spinner1.getSelectedItem()).equals("Small")) {
     String mytext = "Something else ";
     view11.small(mytext); // view11 is clas view1 reference
 }
 else if(String.valueOf(spinner1.getSelectedItem()).equals("Medium")) {
     finish();
 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM