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
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:
- Why am I getting an "Expected Identifier" error?Best solution by Stack Overflow
- Why am I getting error unwrapping Optional if it has a value?Best solution by Stack Overflow
- Why am I getting error on page?Best solution by Yahoo! Answers
- Why am I receiving a malformed text data error and how do I fix it?Best solution by Yahoo! Answers
- Why am I getting a Yahoo Mail Error?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.