0

Im trying to write an app that puts a string in a textview if there is a file in a given url, this is the code and the crash info, what could be the error?.

public class MainActivity extends AppCompatActivity {

String url = "https://images.pexels.com/photos/617278/pexels-photo-617278.jpeg?cs=srgb&dl=adorable-animal-blur-617278.jpg&fm=jpg";

TextView textView;

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

    textView.findViewById(R.id.textView);

        try {
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("HEAD");
            connection.connect();

            if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){

                textView.setText("File Exists!!");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.archivoexiste, PID: 6160 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.archivoexiste/com.example.android.archivoexiste.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference at com.example.android.archivoexiste.MainActivity.onCreate(MainActivity.java:23) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  Application terminated.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

1

Change this line in your oncreate method

 textView.findViewById(R.id.textView);

to this

textView = (TextView) findViewById(R.id.textView);
ShehrozEK
  • 180
  • 1
  • 1
  • 6
  • 1
    Thanks, I have review some results in google and I did fix it, the issue was that the network calls must be inside an AsyntTask. – dionisio70 Dec 08 '18 at 08:30