Reputation: 1771
I'm stuck. I've spent hours looking at this and I know it must be something trivial I have done wrong. My native "tongue" is C++, but I wanted to work with Android. I really like using structs in C++, but discovered I had to use a work around using a class in Java. I've fairly well narrowed down that I am getting a nullpointer because of something wrong with my class. Any ideas?
Here is the code with line 267 marked:
public class Vectors extends Activity{
Button next;
public class infoC{
double value = 0, angle = 0;
boolean radian = false; //radians/degrees
int quad = 1; //Quadrant
}
public infoC[] info;
int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.vectors);
vectorsView = new VectorsView(this);
l = (LinearLayout) findViewById(R.id.llCanvasV);
l.addView(vectorsView);
Initialize();
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
error.setText("");
if(value.getText().toString().length()==0||value.getText()==null||angle.getText().toString().length()==0||angle.getText()==null)
error.setText("Must enter both value and angle");
else{
Log.e("Counter", Integer.toString(counter));
Log.e("Value", value.getText().toString());
storeInfo(); //121
counter++;
}
}
});
}
public void storeInfo(){
Doublify(value);
Doublify(angle);
String temp;
temp=value.getText().toString();
info[counter].value = Double.parseDouble(temp);//267, Inserted temp instead of getText.toString
info[counter].angle = Double.parseDouble(angle.getText().toString());
info[counter].radian = rad.isChecked();
if(q1.isChecked())
info[counter].quad=1;
if(q2.isChecked())
info[counter].quad=2;
if(q3.isChecked())
info[counter].quad=3;
if(q4.isChecked())
info[counter].quad=4;
angle.setText("");
value.setText("");
}
}
Here is the logcat:
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): FATAL EXCEPTION: main
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): java.lang.NullPointerException
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at com.prattia.webs.cheaterphysics.Vectors.storeInfo(Vectors.java:267)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at com.prattia.webs.cheaterphysics.Vectors$7.onClick(Vectors.java:121)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.view.View.performClick(View.java:2465)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.view.View$PerformClick.run(View.java:8907)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.os.Handler.handleCallback(Handler.java:587)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.os.Looper.loop(Looper.java:123)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at java.lang.reflect.Method.invoke(Method.java:521)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 158
Reputation: 1200
define variable l as LinearLayout before using it
this on your variable declarations, also you should declare your vectorview as view
Upvotes: 0
Reputation: 1628
info has to be initialized before you use it.
public infoC[] info = new infoC[10];
I'm pretty sure that's how it works in C++ too. Of course I don't know how many elements you will need, I put 10. So you might want to use something dynamic like:
...
public ArrayList<infoC> info = new ArrayList<infoC>();
...
info.add(new infoC());
info.get(counter).value = Double.parseDouble(temp);
info.get(counter).angle = Double.parseDouble(angle.getText().toString());
info.get(counter).radian = rad.isChecked();
....
Upvotes: 0
Reputation: 2511
You should just debug your code, place a breakpoint at line 267 and see what the values of your variables are. Are you sure your info is not null? I can't see any initialization being done on it.
Upvotes: 0
Reputation: 722
Could it be because you never initialize your array public infoC[] info = new infoC[size];
?
Upvotes: 0
Reputation: 2854
It doesn't look like you ever initialized your info[] array. Thus info[counter] throws an exception.
Upvotes: 1