Why do I have a NullPointerException?

In android programming, why am I getting a NullPointerException error?

  • I'm fairly new to android programming and I don't understand why I am getting this error. Here is part of the logcat: 03-26 20:41:37.196: ERROR/AndroidRuntime(8250): FATAL EXCEPTION: main 03-26 20:41:37.196: ERROR/AndroidRuntime(8250): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prattia.webs.cheaterph... java.lang.NullPointerException 03-26 20:41:37.196: ERROR/AndroidRuntime(8250): Caused by: java.lang.NullPointerException 03-26 20:41:37.196: ERROR/AndroidRuntime(8250): at com.prattia.webs.cheaterphysics.Pythag.o... And here is the segment of the code I think it is: public class Pythag extends Activity { Button submit, clear; EditText at, bt, ct; TextView error, afinal, bfinal, cfinal; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.formulas); submit = (Button) findViewById(R.id.bSubmitP); clear = (Button) findViewById(R.id.bClearP); at = (EditText) findViewById(R.id.etSideAP); bt = (EditText) findViewById(R.id.etSideBP); ct = (EditText) findViewById(R.id.etSideCP); error = (TextView) findViewById(R.id.tvErrorP); afinal = (TextView) findViewById(R.id.tvSideAP); bfinal = (TextView) findViewById(R.id.tvSideBP); cfinal = (TextView) findViewById(R.id.tvSideCP); submit.setOnClickListener(new View.OnClickListener() { //line 33 @Override public void onClick(View v) { // TODO Auto-generated method stub String as, bs, cs; double ad, bd, cd; int counter = 0; if(at.getText().length()==0) counter++; if(bt.getText().length()==0) counter++; if(ct.getText().length()==0) counter++; if(counter!=1){ error.setText("Enter exactly two values"); } else { as = at.getText().toString(); bs = bt.getText().toString(); cs = ct.getText().toString(); ad = Double.valueOf(as); bd = Double.valueOf(bs); cd = Double.valueOf(cs); if(ad<0 || bd<0 || cd<0) { error.setText("Sides must be positive"); } else { if(at.getText().length()==0){ bfinal.setText(bt.getText()); cfinal.setText(ct.getText()); ad = Math.pow((Math.pow(cd, 2)-Math.pow(bd, 2)), .5); afinal.setText(Double.toString(... } if(bt.getText().length()==0){ afinal.setText(at.getText()); cfinal.setText(ct.getText()); bd = Math.pow((Math.pow(cd, 2)-Math.pow(ad, 2)), .5); bfinal.setText(Double.toString(... } if(at.getText().length()==0){ afinal.setText(at.getText()); bfinal.setText(bt.getText()); cd = Math.pow((Math.pow(ad, 2)+Math.pow(bd, 2)), .5); cfinal.setText(Double.toString(... } } } } });

  • Answer:

    I also just started with android so I can't be 100% sure, but it seems you need to create an object of Pythag before you try to start it. Somewhere in your code you say start Pythag activity, but before that you likely need to define and initialize a Pythag object. Pythag pythagActivity = new Pythag(); Otherwise check your imports and package names. I know the folder you store certain parts in matters, so maybe its in the wrong folder. Just a couple of troubleshooting suggestions, good luck :)

Reese Pratt at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

I also just started with android so I can't be 100% sure, but it seems you need to create an object of Pythag before you try to start it. Somewhere in your code you say start Pythag activity, but before that you likely need to define and initialize a Pythag object. Pythag pythagActivity = new Pythag(); Otherwise check your imports and package names. I know the folder you store certain parts in matters, so maybe its in the wrong folder. Just a couple of troubleshooting suggestions, good luck :)

coachabower

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.