简体   繁体   中英

New to android studio and getting a null pointer exception

The Main class of my App

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("button","onCreate: Starting");

        Button btn = (Button) findViewById(R.id.GoNextScreen);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("button","onCreate: pressed");
                Intent intent = new Intent(MainActivity.this,OutputResult.class);
                startActivity(intent);
            }
        });
    }
}

The Output class of my App

package newapptry.com.permutation;

import android.content.Intent;
import android.icu.util.Output;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class OutputResult extends AppCompatActivity   {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.output_result);

        EditText Word = (EditText) findViewById(R.id.InputWord);

        TextView output = (TextView) findViewById(R.id.OutputTextView);

        String input = (String) Word.getText().toString();

        output.setText(input);
        Log.d("button","onCreate: Starting");
        Button back = (Button) findViewById(R.id.BackButton);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("button","onCreate: pressed!");
                Intent ins = new Intent(OutputResult.this,MainActivity.class);
                startActivity(ins);
            }
        });

    }
}

Explanation : The goal of this app is to take an user's input from the input page, and then pass it on to the output page and print it.

The Problem : After several tries, I checked the log and figured that I am getting a null pointer exception in the output class of my app at

String input = (String) Word.getText().toString();

LogCat

09-29 15:32:42.992 17233-17233/? I/art: Late-enabling -Xcheck:jni
09-29 15:32:43.132 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.422 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.492 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.572 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.642 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.722 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.812 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.882 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:43.962 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:44.032 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:44.102 17233-17233/newapptry.com.permutation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a7 --instruction-set-features=default --dex-file=/data/app/newapptry.com.permutation-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@newapptry.com.permutation-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
09-29 15:32:44.122 17233-17233/newapptry.com.permutation W/System: ClassLoader referenced unknown path: /data/app/newapptry.com.permutation-1/lib/arm
09-29 15:32:44.122 17233-17233/newapptry.com.permutation I/InstantRun: starting instant run server: is main process
09-29 15:32:44.272 17233-17233/newapptry.com.permutation W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@9542280
09-29 15:32:44.372 17233-17233/newapptry.com.permutation V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@a55e0b9
09-29 15:32:44.392 17233-17233/newapptry.com.permutation I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
09-29 15:32:44.392 17233-17233/newapptry.com.permutation I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
09-29 15:32:44.522 17233-17233/newapptry.com.permutation D/button: onCreate: Starting
09-29 15:32:44.552 17233-17277/newapptry.com.permutation D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-29 15:32:44.602 17233-17277/newapptry.com.permutation I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8909_LA.BR.1.2.6_RB1__release_AU (Ib683e2651b)
    OpenGL ES Shader Compiler Version: E031.29.00.00
    Build Date: 03/30/16 Wed
    Local Branch: 
    Remote Branch: quic/LA.BR.1.2.6_rb1.12
    Local Patches: NONE
    Reconstruct Branch: NOTHING
09-29 15:32:44.602 17233-17277/newapptry.com.permutation I/OpenGLRenderer: Initialized EGL, version 1.4
09-29 15:32:44.702 17233-17233/newapptry.com.permutation W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
09-29 15:32:50.602 17233-17233/newapptry.com.permutation D/Settings: start to get string for name : sound_effects_enabled , userHandler : 0
09-29 15:32:50.602 17233-17233/newapptry.com.permutation V/Settings: invalidate [system]: current 10 != cached 0
09-29 15:32:50.602 17233-17233/newapptry.com.permutation D/Settings: start to get string with lazy provider
09-29 15:32:50.612 17233-17233/newapptry.com.permutation D/Settings: return value in bundle is not null
09-29 15:32:50.612 17233-17233/newapptry.com.permutation D/button: onCreate: pressed
09-29 15:32:50.682 17233-17233/newapptry.com.permutation V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@7a54e01
09-29 15:32:50.682 17233-17233/newapptry.com.permutation V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@4ecb4a6
09-29 15:32:50.702 17233-17233/newapptry.com.permutation D/AndroidRuntime: Shutting down VM
09-29 15:32:50.702 17233-17233/newapptry.com.permutation E/AndroidRuntime: FATAL EXCEPTION: main
    Process: newapptry.com.permutation, PID: 17233
    java.lang.RuntimeException: Unable to start activity ComponentInfo{newapptry.com.permutation/newapptry.com.permutation.OutputResult}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.access$900(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:171)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at newapptry.com.permutation.OutputResult.onCreate(OutputResult.java:25)
        at android.app.Activity.performCreate(Activity.java:6285)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.access$900(ActivityThread.java:150) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:171) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
09-29 15:32:52.122 17233-17233/newapptry.com.permutation I/Process: Sending signal. PID: 17233 SIG: 9

You are trying to get the user input after declaration so is allways null, you need to move String input = (String) Word.getText().toString(); inside the onclick callback, and its optional, but you can put a nullity check or a try/catch to avoid null pointer if the user don't write anything

No need for (String) :

String input = Word.getText().toString();

Update:

android.text.Editable android.widget.EditText.getText()' on a null object reference

It's getting null because the EditText you're trying to get string from is empty. That's why.

Also, you're setting the string to the previous variable:

output.setText(input);

Follow this:

Use this in onClick method:

String input = Word.getText().toString();
Intent intent = new Intent(youractivity.this, AnotherActivity.class);
intent.putExtra("YOUR_EXTRA", input);
startActivity(intent);

And to get in another Activity , use below codes inside onCreate() :

String yourOutput = getIntent().getStringExtra("YOUR_EXTRA");

Rohan. If I understood you correctly, then you need use TextWatcher for your EditText. You got NullPointerException, because your EditText is empty. The TextWatcher will say you when text in editText changed.

只需使用它从 EditText 获取文本作为字符串

 output.setText(Word.getText().toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM