Java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean java.lang.Boolean.booleanValue()'

时间:2015-11-07 00:07:05

标签: java android nullpointerexception

所以我正在编写一个翻译应用程序,这个错误仍然存​​在。 这是代码,请帮忙!

RAW CODE 1

public class MyActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE"; //Defining EXTRA MESSAGE
    public static Boolean hisCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        ToggleButton toggle = (ToggleButton) findViewById(R.id.his);

    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
        if(isChecked){
            hisCheck = true; 
        }
        else {
            hisCheck = false; 
        }
    }


    public void sendMessage(View view){
        Intent intent = new Intent("com.robertb.TRANSLATED");
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);

    }

}

RAW CODE 2

    super.onCreate(savedInstanceState);
      // Get the message from the intent
    Intent intent = getIntent();
    String input = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
    Set<String> output = new HashSet<String>();
    String[] outputS = new String[500];
    Boolean check = MyActivity.hisCheck;
   // output[0] = "There are no detected gender specific words within your submitted message";
    int j = 0;
    String outputFinal;;

    List<String> lines = new ArrayList<String>();
    List<String> lines2 = new ArrayList<String>();
    AssetManager assetManager = getAssets();


 //goodwords array
    try {
        InputStream goodIs = null;
        if(check){
        goodIs = assetManager.open("goodwords.txt");
        }
        if(check){
            goodIs = assetManager.open("goodwordshis.txt");
            }
        BufferedReader reader = new BufferedReader(new InputStreamReader(goodIs));

        String line;

        while ((line = reader.readLine()) != null) {

            lines2.add(line);

        }


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


  //badwords array
    try {
        InputStream badIs = null; 
        if(!check){
        badIs = assetManager.open("badwords.txt");
        }if(check){
         badIs = assetManager.open("badwordshis.txt");
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(badIs));
        String line1;

        while ((line1 = reader.readLine()) != null) {

            lines.add(line1);

        }
        String[] arrayBad = lines.toArray(new String[0]);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String[] arrayGood = lines2.toArray(new String[0]);
    String[] arrayBad = lines.toArray(new String[0]);

    String[] words = input.split("\\W");



    for(int i1 = 0; i1 < words.length; ++i1){
         for(int i = 0; i < arrayBad.length; ++i)
         {
             if(words[i1].equalsIgnoreCase(arrayBad[i])){
                 output.add(arrayBad[i] + " = " + arrayGood[i]);

             }
         }
    } 


        StringBuilder out = new StringBuilder();


    for( String s : output) {
        out.append( s  + "\n");
        }
    if(output.isEmpty()){
        out.append("There are no detected gender specific words within your submitted message");
    }
    outputFinal = out.toString();
    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(outputFinal);

    // Set the text view as the activity layout
    setContentView(textView);


    }
 }

logcat的

    11-06 19:55:40.031: E/AndroidRuntime(28865): FATAL EXCEPTION: main
11-06 19:55:40.031: E/AndroidRuntime(28865): Process: com.example.android, PID: 28865
11-06 19:55:40.031: E/AndroidRuntime(28865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.Translated}: java.lang.NullPointerException: lock == null
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.os.Looper.loop(Looper.java:155)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.app.ActivityThread.main(ActivityThread.java:5696)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at java.lang.reflect.Method.invoke(Native Method)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at java.lang.reflect.Method.invoke(Method.java:372)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
11-06 19:55:40.031: E/AndroidRuntime(28865): Caused by: java.lang.NullPointerException: lock == null
11-06 19:55:40.031: E/AndroidRuntime(28865):    at java.io.Reader.<init>(Reader.java:64)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at java.io.InputStreamReader.<init>(InputStreamReader.java:120)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at java.io.InputStreamReader.<init>(InputStreamReader.java:57)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at com.example.android.Translated.onCreate(Translated.java:51)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.app.Activity.performCreate(Activity.java:5958)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
11-06 19:55:40.031: E/AndroidRuntime(28865):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
11-06 19:55:40.031: E/AndroidRuntime(28865):    ... 10 more

如果您希望我筛选并返回重要的布尔部分,那么我可以这样做。请问。这似乎是一个非常基本的错误,我真的需要帮助。谢谢!我需要在if(num!= null)中添加的任何想法,或者我是否需要添加?

2 个答案:

答案 0 :(得分:0)

您确定此行绝对正确await吗?

你想做什么?

ConduitM

最后,您是否在androidManifest中定义了这个新活动?

答案 1 :(得分:0)

如果您通过意图传递信息,请使用 startActivityForResult(intent)代替startActivity(intent)