gpt4 book ai didi

Java.lang.NullPointerException : Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference

转载 作者:行者123 更新时间:2023-12-01 06:11:37 25 4
gpt4 key购买 nike

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

原始代码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);

}

}

原始代码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);


}
}

日志记录

    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

如果您希望我对此进行筛选并仅返回重要的 boolean 部分,那么我可以这样做。就问吧。这似乎犯了一个非常基本的错误,我真的非常需要帮助。谢谢!我需要添加 if(num != null) 的任何想法,或者我是否需要添加?

最佳答案

如果您通过 Intent 传递信息,则使用 <strong>startActivityForResult(intent)</strong>而不是<strong>startActivity(intent)</strong>

关于Java.lang.NullPointerException : Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33577525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com