java - Android Application Crashes when Creating custom class object -
i'm working on first android app , i'm having trouble creating object of class have programmed in java using oncreate function of activity. activity starts great when not instantiate object, when attempt create object app crashes when switching activity. oncreate function looks this...
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_entry); // show button in action bar. setupactionbar(); forcetable testtable = new forcetable(); double factor = testtable.returnvalue(forcetypes.newtons, forcetypes.newtons); }
forcetable class have programmed, code looks this...
public class forcetable { private double[][] forcetable; protected enum forcetypes {newtons(0), pounds(1), kilopond(2); public int num; private forcetypes(int num) { this.num = num; } }; protected final class values{ private final static double zerozero = 1.00; private final static double zeroone = 4.44872162; private final static double zerotwo = 9.80665; private final static double onezero = .224808943; private final static double oneone = 1.00; private final static double onetwo = 2.20462262; private final static double twozero = .10197164; private final static double twoone = .45359237; private final static double twotwo = 1.00; } public forcetable() { this.maketable(); } private void maketable() { forcetable[0][0] = values.zerozero; forcetable[0][1] = values.zeroone; forcetable[0][2] = values.zerotwo; forcetable[1][0] = values.onezero; forcetable[1][1] = values.oneone; forcetable[1][2] = values.onetwo; forcetable[2][0] = values.twozero; forcetable[2][1] = values.twoone; forcetable[2][2] = values.twotwo; } public double returnvalue(forcetypes ifyouhave, forcetypes thenyouhave){ double factor = forcetable[thenyouhave.num][ifyouhave.num]; return factor; }
}
it's been long time since have programmed in java, , since activity starts fine without instantiating must java code forcetable. notice wrong? there's chance it's easy i'm not brushed on...
here logcat
08-08 18:08:13.206: e/(9801): : can't open file reading 08-08 18:08:13.206: e/(9801): : can't open file reading 08-08 18:10:34.045: w/dalvikvm(9801): threadid=1: thread exiting uncaught exception (group=0x4136b438) 08-08 18:10:34.045: e/androidruntime(9801): fatal exception: main 08-08 18:10:34.045: e/androidruntime(9801): java.lang.runtimeexception: unable start activity componentinfo{khandy.application.convertible/khandy.application.convertible.entryactivity}: java.lang.nullpointerexception 08-08 18:10:34.045: e/androidruntime(9801): @ android.app.activitythread.performlaunchactivity(activitythread.java:2110) 08-08 18:10:34.045: e/androidruntime(9801): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2135) 08-08 18:10:34.045: e/androidruntime(9801): @ android.app.activitythread.access$700(activitythread.java:143) 08-08 18:10:34.045: e/androidruntime(9801): @ android.app.activitythread$h.handlemessage(activitythread.java:1241) 08-08 18:10:34.045: e/androidruntime(9801): @ android.os.handler.dispatchmessage(handler.java:99) 08-08 18:10:34.045: e/androidruntime(9801): @ android.os.looper.loop(looper.java:137) 08-08 18:10:34.045: e/androidruntime(9801): @ android.app.activitythread.main(activitythread.java:4950) 08-08 18:10:34.045: e/androidruntime(9801): @ java.lang.reflect.method.invokenative(native method) 08-08 18:10:34.045: e/androidruntime(9801): @ java.lang.reflect.method.invoke(method.java:511) 08-08 18:10:34.045: e/androidruntime(9801): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1004) 08-08 18:10:34.045: e/androidruntime(9801): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:771) 08-08 18:10:34.045: e/androidruntime(9801): @ dalvik.system.nativestart.main(native method) 08-08 18:10:34.045: e/androidruntime(9801): caused by: java.lang.nullpointerexception 08-08 18:10:34.045: e/androidruntime(9801): @ khandy.application.convertible.forcetable.maketable(forcetable.java:34) 08-08 18:10:34.045: e/androidruntime(9801): @ khandy.application.convertible.forcetable.(forcetable.java:29) 08-08 18:10:34.045: e/androidruntime(9801): @ khandy.application.convertible.entryactivity.oncreate(entryactivity.java:21) 08-08 18:10:34.045: e/androidruntime(9801): @ android.app.activity.performcreate(activity.java:5179) 08-08 18:10:34.045: e/androidruntime(9801): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1094) 08-08 18:10:34.045: e/androidruntime(9801): @ android.app.activitythread.performlaunchactivity(activitythread.java:2074) 08-08 18:10:34.045: e/androidruntime(9801): ... 11 more
you nullpointerexception @ line
forcetable[0][0] = values.zerozero;
because try access array without creating first. before accessing it, create that
forcetable = new double[][]; forcetable[0][0] = values.zerozero;
and please post logcat output! makes easier find problem.
Comments
Post a Comment