问题
I've a error thrown I can't figure out how it happens. It doesn't occur all the time but once in a while. The error message thrown is "Java.lang.nullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference". I've used connectionRequest for parsing the json data. When the error is thrown I've checked if there's problem in url but it gives the proper json data as always. After trying few times, the error is solved automatically.
if (!regNOField.getText().equals("") && !newPasswordField.getText().equals("")) {
ArrayList<Map<String, String>> argumentsArrayList = new ArrayList<>();
Map argumentMap1 = new HashMap();
argumentMap1.put("argument", "reg_no");
argumentMap1.put("value", regNOField.getText().trim());
argumentsArrayList.add(argumentMap1);
Map argumentMap2 = new HashMap();
argumentMap2.put("argument", "token");
argumentMap2.put("value", newPasswordField.getText().trim());
argumentsArrayList.add(argumentMap2);
CommonMethod.connectionMethod(res, true, url, "Verification", argumentsArrayList, true, null);
}
CommonMethod.connectionMethod:
public static void connectionMethod(Resources res, Boolean postBoolean, String urlString, String form, ArrayList<Map<String, String>> arguments,
Boolean ipFlag, String extraData) {
ConnectionRequest cr = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser jp = new JSONParser();
parser = jp.parseJSON(new InputStreamReader(input));
response = null;
if (parser != null && !parser.isEmpty()) {
if (parser.get("status").equals("true")) {
message = (String) parser.get("data");
ToastBar.getInstance().setPosition(Component.TOP);
ToastBar.showErrorMessage(message, 5000);
if (form.equals("Verification")) {
if (d != null) {
d.dispose();
}
new LoginAndSignUp(res, message).show();
}
}
}
}
@Override
protected void handleErrorResponseCode(int code, String message) {
System.out.println("login ErrorResponseCode " + code + "msg: " + message);
}
@Override
protected void handleException(Exception err) {
System.out.println("login Exception " + err);
}
@Override
protected void handleIOException(IOException err) {
System.out.println("login IOException " + err);
}
@Override
protected void handleRuntimeException(RuntimeException err) {
System.out.println("RuntimeException " + err);
}
};
cr.setPost(postBoolean);
cr.setUrl(urlString);
cr.setTimeout(80000);
if (arguments != null && !postBoolean.equals(false)) {
for (Map<String, String> entrySet : arguments) {
String argument1 = (String) entrySet.get("argument");
String value1 = (String) entrySet.get("value");
cr.addArgument(argument1, value1);
}
}
InfiniteProgress ip = new InfiniteProgress();
d = ip.showInifiniteBlocking();
cr.setDisposeOnCompletion(d);
cr.addRequestHeader("accept", "application/json");
NetworkManager.getInstance().addToQueueAndWait(cr);
}
Error msg:
来源:https://stackoverflow.com/questions/53358382/cn1-attempt-to-invoke-virtual-method-boolean-java-util-arraylist-addjava-lan