-1

everytime I'm trying to start my app it crashes immediately on this part of code.

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isOnline()) {
                   // textViewTemperature.setText(Data);
              Toast.makeText(getApplicationContext(), "Cannot connect to network, data will be restore from file with last downloaded data...", Toast.LENGTH_LONG).show();
                } else {
                    Thread t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                weather = parser.getWeatherForLocation(editText.getText() + "");
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        editText.setText(weather.getCity() + ",  " + weather.getCountry());    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    t.start();
                }
            }
        });

This is the full logcat from this error:

12-29 10:06:36.376 3524-3524/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.piotr.pogoda, PID: 3524
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.piotr.pogoda/com.example.piotr.pogoda.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                     at com.example.piotr.pogoda.MainActivity.onCreate(MainActivity.java:136)
                                                     at android.app.Activity.performCreate(Activity.java:6662)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:154) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

I can see the error but I don't get what does it mean that I'm trying to use a method on a null object reference. The button does not exist or what?

PiiiTeeeR
  • 45
  • 3

3 Answers3

0

Initialize your button, either by findViewById(R.id.btn) or programatically button = new Button(getApplicationContext())

Greggz
  • 1,873
  • 1
  • 12
  • 31
0

From your crash log, button view is null. May be your view initialization has some issues. Please check the code.

EKN
  • 1,886
  • 1
  • 16
  • 29
0

You need to initialize the button

Button btn = (Button) findViewById()
AskNilesh
  • 67,701
  • 16
  • 123
  • 163