-4

This is my code, but I had an error in logcat

com.example.norhanom.example2 E/AndroidRuntime: FATAL EXCEPTION: main com.example.norhanom.example2 E/AndroidRuntime: Process: com.example.norhanom.example2, PID: 26334 com.example.norhanom.example2 E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference com.example.norhanom.example2 E/AndroidRuntime: at com.example.norhanom.example2.MainActivity$2.onErrorResponse(MainActivity.java:68) com.example.norhanom.example2 E/AndroidRuntime: at com.android.volley.Request.deliverError(Request.java:564) com.example.norhanom.example2 E/AndroidRuntime: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101) com.example.norhanom.example2 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:815) com.example.norhanom.example2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:104) com.example.norhanom.example2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:207) com.example.norhanom.example2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5777) com.example.norhanom.example2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) com.example.norhanom.example2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) com.example.norhanom.example2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) com.example.norhanom.example2 I/Process: Sending signal. PID: 26334 SIG: 9

 MainActivity
package com.example.norhanom.example2;

import android.app.ProgressDialog;
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;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {

private EditText editTextBarcodeNumber;
private Button buttonGet;
private TextView textViewResult;

private ProgressDialog loading;



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

    editTextBarcodeNumber = (EditText) findViewById(R.id.editTextbc);
    buttonGet = (Button) findViewById(R.id.button);
    textViewResult = (TextView) findViewById(R.id.textViewResult);

    buttonGet.setOnClickListener(this);

}

private void getData() {
    String bc = editTextBarcodeNumber.getText().toString().trim();
    if (bc.equals("")) {
        Toast.makeText(this, "Please enter the barcode number", 
     Toast.LENGTH_LONG).show();
        return;
    }
    loading = ProgressDialog.show(this,"Please 
     wait...","Fetching...",false,false);

    String url = 
 Config.DATA_URL+editTextBarcodeNumber.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(url, new 
 Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String response){
    String name="";
    String StatusProduct="";
    String ExpiredDate = "";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject halal_data = result.getJSONObject(0);
        name = halal_data.getString(Config.KEY_NAME);
        StatusProduct = halal_data.getString(Config.KEY_STATUSPRODUCT);
        ExpiredDate = halal_data.getString(Config.KEY_EXPIREDDATE);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    textViewResult.setText("Name:\t" + name + "\nStatus of Product:\t" + 
 StatusProduct + "\nExpired Date:\t"+ ExpiredDate);
}

@Override
public void onClick(View v) {
    getData();
}
}


Config.java
 package com.example.norhanom.example2;


 public class Config {
public static final String DATA_URL = 
"http://192.168.1.7/android/getData.php?BarcodeNumber=";
public static final String KEY_NAME = "Name";
public static final String KEY_STATUSPRODUCT = "StatusProduct";
public static final String KEY_EXPIREDDATE = "ExpiredDate";
public static final String JSON_ARRAY = "result";
}


activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GET"
    android:id="@+id/button"
    android:layout_below="@+id/editTextbc"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editTextbc"
    android:text="enter the barcode number"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="55dp" />

<TextView
    android:id="@+id/textViewResult"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="55dp" />

   [enter image description here][1]
bella
  • 13
  • 5

1 Answers1

0

As you can see the error in your toast, specifically at:

error.getMessage().toString()

The error parameter might not always have a message value, and could return a null.

To fix this, put the following check before showing the toast:

If (error.getMessage() != null) { // show toast }
Abdul Wasae
  • 3,614
  • 4
  • 34
  • 56
  • i had to put at here ? – bella Oct 17 '17 at 14:14
  • sorry. i had put here ? public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); } – bella Oct 17 '17 at 14:14