Every time I run my app it keeps crashing immediately and throwing a NullPointerException
. I can't figure out where or why though.
LogCat:
10-13 02:23:05.028: E/AndroidRuntime(1610): FATAL EXCEPTION: main
10-13 02:23:05.028: E/AndroidRuntime(1610): Process: com.association.icecreammafiatesting3, PID: 1610
10-13 02:23:05.028: E/AndroidRuntime(1610): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.association.icecreammafiatesting3/com.association.icecreammafiatesting3.MainActivity}: java.lang.NullPointerException
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.os.Looper.loop(Looper.java:136)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-13 02:23:05.028: E/AndroidRuntime(1610): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 02:23:05.028: E/AndroidRuntime(1610): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 02:23:05.028: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-13 02:23:05.028: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-13 02:23:05.028: E/AndroidRuntime(1610): at dalvik.system.NativeStart.main(Native Method)
10-13 02:23:05.028: E/AndroidRuntime(1610): Caused by: java.lang.NullPointerException
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.Activity.findViewById(Activity.java:1884)
10-13 02:23:05.028: E/AndroidRuntime(1610): at com.association.icecreammafiatesting3.MainActivity.<init>(MainActivity.java:19)
10-13 02:23:05.028: E/AndroidRuntime(1610): at java.lang.Class.newInstanceImpl(Native Method)
10-13 02:23:05.028: E/AndroidRuntime(1610): at java.lang.Class.newInstance(Class.java:1208)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
10-13 02:23:05.028: E/AndroidRuntime(1610): ... 11 more
and here is the MainActivity:
public class MainActivity extends FragmentActivity {
private int totalMoney = 0;
private int moneyPerSec = 1;
private int moneyPerClick = 1;
Handler handleCounter = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView counter = (TextView)findViewById(R.id.tvMoney);
TextView perSecCounter = (TextView)findViewById(R.id.tvMonPerSec);
Button buttonMoney = (Button)findViewById(R.id.buttonMoney);
Button buttonMoneyPerSec = (Button)findViewById(R.id.buttonMoneyPerSec);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
handleCounter.post(updateCounter);
buttonMoney.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
totalMoney += moneyPerClick;
counter.setText(totalMoney);
}
});
buttonMoneyPerSec.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (totalMoney >= 10){
totalMoney -= 10;
counter.setText(totalMoney);
moneyPerSec++;
}
}
});
}
Runnable updateCounter = new Runnable(){
public void run(){
totalMoney += moneyPerSec;
counter.setText(totalMoney);
handleCounter.postDelayed(this, 1000);
}
};
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos){
switch(pos){
case 0: return StandFragment.newInstance("STAND");
case 1: return CartFragment.newInstance("CART");
case 2: return ThirdFragment.newInstance("THIRD");
case 3: return ThirdFragment.newInstance("FOURTH");
case 4: return ThirdFragment.newInstance("FIFTH");
default: return ThirdFragment.newInstance("Default");
}
}
@Override
public int getCount(){
return 5;
}
}
}
Changed where I was initializing, still throwing the same error. Here's the updated code:
public class MainActivity extends FragmentActivity {
private int totalMoney = 0;
private int moneyPerSec = 1;
private int moneyPerClick = 1;
TextView counter;
TextView perSecCounter;
Handler handleCounter = new Handler();
Button buttonMoney;
Button buttonMoneyPerSec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonMoney = (Button)findViewById(R.id.buttonMoney);
buttonMoneyPerSec = (Button)findViewById(R.id.buttonMoneyPerSec);
counter = (TextView)findViewById(R.id.tvMoney);
perSecCounter = (TextView)findViewById(R.id.tvMonPerSec);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
handleCounter.post(updateCounter);
buttonMoney.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
totalMoney += moneyPerClick;
counter.setText(totalMoney);
}
});
buttonMoneyPerSec.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (totalMoney>=10){
totalMoney-=10;
counter.setText(totalMoney);
moneyPerSec++;
}
}
});
}
Updated LogCat:
10-13 12:21:46.236: E/AndroidRuntime(4323): FATAL EXCEPTION: main
10-13 12:21:46.236: E/AndroidRuntime(4323): Process: com.association.icecreammafiatesting3, PID: 4323
10-13 12:21:46.236: E/AndroidRuntime(4323): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.association.icecreammafiatesting3/com.association.icecreammafiatesting3.MainActivity}: java.lang.NullPointerException
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.os.Looper.loop(Looper.java:136)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-13 12:21:46.236: E/AndroidRuntime(4323): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 12:21:46.236: E/AndroidRuntime(4323): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 12:21:46.236: E/AndroidRuntime(4323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-13 12:21:46.236: E/AndroidRuntime(4323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-13 12:21:46.236: E/AndroidRuntime(4323): at dalvik.system.NativeStart.main(Native Method)
10-13 12:21:46.236: E/AndroidRuntime(4323): Caused by: java.lang.NullPointerException
10-13 12:21:46.236: E/AndroidRuntime(4323): at com.association.icecreammafiatesting3.MainActivity.onCreate(MainActivity.java:40)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.Activity.performCreate(Activity.java:5231)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-13 12:21:46.236: E/AndroidRuntime(4323): ... 11 more
As mentioned in one of the answers below, could this be due to trying to initialize in the main activity an object that is in a fragment? Because I'm using a pageViewer to have multiple screens. If so, I have no idea how I would move that code to that fragment. Because it won't allow me to initialize using findByID there.
That was the cause. After moving the code to the fragment, I added v. before the findViewById and it worked. Also, I remembered about an error I had previously. It won't let me assign a variable to text, I have to do .setText(""+variable).