2

I'm new to both Java and Android Studio (1 day's experience in each).

I'm making a test app where you tap a walnut and a counter goes up by one each time, pretty simple. The app was running perfectly fine, however, this morning it stopped running properly, and the app stops whenever I try to run it.

I've tried a couple things and have narrowed it down to a line and an error, but I'm not sure what to make of it.

I understand what a NullPointerException is, but I have no idea how to fix it in this case. Since I'm new to the IDE and language, any suggestions about anything in the code is absolutely welcome too!

The app is made up of 2 classes as of now:

MainActivity.java :

package com.nut.calendar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{
    //WIDGETS
    ImageButton nutButton = findViewById(R.id.nutButton);
    TextView nutNum = findViewById(R.id.nutsNum);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Variables.nutCount += 1;
                nutNum.setText(String.valueOf(Variables.nutCount));
            }
        });
    }

}

Variables.java :

package com.nut.calendar;

public class Variables{
    public static int nutCount = 0;
}

The error points to line 12 in 'MainActivity.java' :

ImageButton nutButton = findViewById(R.id.nutButton);

And the error in Logcat says :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

I would really appreciate any input!

Here's the whole Stack Trace: Error

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Nathan
  • 145
  • 1
  • 7
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stultuske Jan 06 '21 at 14:36
  • Usually Nullpointers are the esiest to fix. Please show us the whole stack trace, as it would points to the function that leads to the nullpointer. – ChristianB Jan 06 '21 at 14:39
  • Ah, I've already seen this one. I'm not sure what I'm looking for in my case, but thanks! – Nathan Jan 06 '21 at 14:40
  • @ChristianB sure – Nathan Jan 06 '21 at 14:43
  • 1
    `findViewById` will return null if you call it before call `setContentView`. So "nutButton" and "nutNum" are null – Sinner of the System Jan 06 '21 at 14:45
  • @SinneroftheSystem Thanks! This fixed the issue. Does this mean I will need to make the variable and define each widget every time the app launches? – Nathan Jan 06 '21 at 14:53
  • you should use [view binding](https://developer.android.com/topic/libraries/view-binding) – Sinner of the System Jan 06 '21 at 14:56
  • @Nathan, your question is good, however for the next time I suggest you to try to don’t add a picture, Have a look here => [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557) I thumped up anyway – Federico Baù Jan 06 '21 at 15:03
  • @Nathan I would recommend stop trying to write applications. You said that you just started learning Java one day ago, and if the link I provided doesn't give you an idea of where to start looking, it's clear you have little knowledge of the basics. First learn the basics, then go to more advanced material. – Stultuske Jan 07 '21 at 06:52

2 Answers2

2

If you look at the Android application lifecycle you will understand why you are having this error. When you launch the app, Android will run the methods in AppCompatActivity in a particular way, you need to call findViewById after setContentView(R.layout.activity_main); because it's setContentView that sets your views and findViewById needs it set before it can actually find any view.

Nemo
  • 152
  • 11
0

findViewById will return null if you call it before call setContentView. So "nutButton" and "nutNum" are null. Here I fix the code copy that.

    package com.nut.calendar;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity{
        //WIDGETS
      TextView nutNum;
      ImageButton nutButton;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
         nutButton = findViewById(R.id.nutButton);
         nutNum = findViewById(R.id.nutsNum);
    
            nutButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Variables.nutCount += 1;
                    nutNum.setText(String.valueOf(Variables.nutCount));
                }
            });
        }
    
    }
ahmad bajwa
  • 966
  • 2
  • 10
  • 30