java - Simple calculation returning NullPointer exception... why? -


(i'm new java please forgive ignorance).

i have simple class:

public class bill {     private string total;     private int tip;     private int people;     bill() {         total = "0";         tip=0;         people=0;     }     bill (string total, int people, int tip) {         total = total;         tip = tip;         people = people;     }      //the problem here somewhere.     private double split = (double.parsedouble(total) * ((double)tip / 100)) / (double)people;      public string gettotal() {         return total;     }      public double getsplit() {         return split;     }      public double gettip() {         return tip;     }  } 

when call 'getsplit()' method, runtime crashes nullpointer exception:

09-03 19:37:02.609: e/androidruntime(11325): caused by: java.lang.nullpointerexception 09-03 19:37:02.609: e/androidruntime(11325):    @ java.lang.stringtoreal.parsedouble(stringtoreal.java:244) 09-03 19:37:02.609: e/androidruntime(11325):    @ java.lang.double.parsedouble(double.java:295) 09-03 19:37:02.609: e/androidruntime(11325):    @ williamgill.de.helloworld.bill.<init>(bill.java:21) 09-03 19:37:02.609: e/androidruntime(11325):    @ williamgill.de.helloworld.displaymessageactivity.oncreate(displaymessageactivity.java:38) 

i seem doing wrong casting of types - can't life of me think what.

at time line executes

private double split = (double.parsedouble(total) * ((double)tip / 100)) / (double)people; 

the fields total , others have not been initialized constructor , have default value of null. parsedouble() must throw nullpointerexception if argument null (i cannot verify this, might come dereferencing).

declare field

private double split; 

and assignment in constructor

bill (string total, int people, int tip) {     total = total;     tip = tip;     people = people;     split = (double.parsedouble(total) * ((double)tip / 100)) / (double)people; } 

advice: java non-constant instance variables should start lowercase alphabetical character.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -