0

Im trying to do a filter using mysql BETWEEN statement, so im sending start and end date to my php files and getting a json array result to be placed into ListView. However im getting this error. Pls help. Tqvm in advance.

test.java

public class test extends AppCompatActivity {
private ListView listViewManagerViewReport;

String receiveSpStartDate;
String receiveSpEndDate;

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

    receiveDate();
    listViewManagerViewReport = (ListView)findViewById(R.id.lvManagerViewReportResult);
    sendRequest();
}

public void receiveDate(){
    SharedPreferences preferences = getSharedPreferences("PassingDetails", MODE_PRIVATE);
    receiveSpStartDate = preferences.getString("startDate", "..");
    receiveSpEndDate = preferences.getString("endDate", "..");
}

class CustomListManagerViewReportAdapter extends ArrayAdapter<String> {
    private Activity context;
    private String[] id;

    private String[] name;
    private String[] total;
    private String[] transactionDate;
    private String[] transactionTime;
    private String[] transactionType;

    public CustomListManagerViewReportAdapter(Activity context,String[] id, String[] name, String[] total, String[] transactionDate, String[] transactionTime, String[] transactionType)
    {
        super(context, R.layout.manager_view_report_result_layout, id);

        this.context = context;
        this.id = id;

        this.name = name;
        this.total = total;
        this.transactionDate = transactionDate;
        this.transactionTime = transactionTime;
        this.transactionType = transactionType;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewMVRItem = inflater.inflate(R.layout.manager_view_report_result_layout, null, true);

        TextView textViewid = (TextView)listViewMVRItem.findViewById(R.id.tvMVRid);

        TextView textViewName = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryName);
        TextView textViewTotal = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryTotal);
        TextView textViewTransactionDate = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryDate);
        TextView textViewTransactionTime = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryTime);
        TextView textViewTransactionType = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryType);

        textViewid.setText(id[position]);
        textViewName.setText(name[position]);
        textViewTotal.setText("TOTAL : " +total[position]);
        textViewTransactionDate.setText("DATE : " +transactionDate[position]);
        textViewTransactionTime.setText("TIME : " +transactionTime[position]);
        textViewTransactionType.setText("TYPE : " +transactionType[position]);

        return listViewMVRItem;
    }
}


private void sendRequest(){

    String url = ManagerViewReportJsonWorker.ViewReport_URL;

    final String startDate = receiveSpStartDate;
    final String endDate = receiveSpEndDate;

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    showJSON(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(test.this,error.toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put(ManagerViewReportJsonWorker.KEY_STARTDATE,startDate);
            params.put(ManagerViewReportJsonWorker.KEY_ENDDATE,endDate);
            return params;
        }

    };

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

}

private void showJSON(String json) {
    ManagerViewReportJsonWorker pj = new ManagerViewReportJsonWorker(json);
    pj.parseJSON();
    CustomListManagerViewReportAdapter cl = new CustomListManagerViewReportAdapter(this,
            ManagerViewAllInventoryJsonWorker.id,
            ManagerViewReportJsonWorker.name,
            ManagerViewReportJsonWorker.total,
            ManagerViewReportJsonWorker.transactionDate,
            ManagerViewReportJsonWorker.transactionTime,
            ManagerViewReportJsonWorker.transactionType);
    listViewManagerViewReport.setAdapter(cl);

}
}

php file

<?php

$con = mysqli_connect(HOST,USER,PASS,DB);

if($_SERVER['REQUEST_METHOD']=='POST'){

$startDate = $_POST['poststartdate'];
$endDate = $_POST['postenddate'];


$sql = "SELECT * FROM inventorytransaction WHERE transactiondate BETWEEN $startDate AND '$endDate'";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res))
{
array_push($result,array(
        'id'=>$row[0],
        'name'=>$row[1],
        'total'=>$row[2],
        'transactiondate'=>$row[3],
        'transactiontime'=>$row[4],
        'transactiontype'=>$row[5]));
}

echo json_encode(array("result"=>$result));

mysqli_close($con);

} 
?>

error log

11-26 16:50:23.060 7800-7800/com.example.user.inventorymanagement E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.user.inventorymanagement, PID: 7800
                                                                                java.lang.NullPointerException: storage == null
                                                                                    at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
                                                                                    at java.util.Arrays.asList(Arrays.java:155)
                                                                                    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
                                                                                    at com.example.user.inventorymanagement.test$CustomListManagerViewReportAdapter.<init>(test.java:0)
                                                                                    at com.example.user.inventorymanagement.test.showJSON(test.java:134)
                                                                                    at com.example.user.inventorymanagement.test.access$000(test.java:25)
                                                                                    at com.example.user.inventorymanagement.test$1.onResponse(test.java:107)
                                                                                    at com.example.user.inventorymanagement.test$1.onResponse(test.java:104)
                                                                                    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
                                                                                    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                                                    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                                    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)

1 Answer 1

1

This error occurs when one of your adapter's arrays is null. Check if they are non-null and only then assign them to the adapter.

class CustomListManagerViewReportAdapter extends ArrayAdapter<String> {
private Activity context;
private String[] id;

private String[] name = new String[] {""};
private String[] total = new String[] {""};
private String[] transactionDate = new String[] {""};
private String[] transactionTime = new String[] {""};
private String[] transactionType = new String[] {""};

public CustomListManagerViewReportAdapter(Activity context,String[] id, String[] name, String[] total, String[] transactionDate, String[] transactionTime, String[] transactionType)
{
    super(context, R.layout.manager_view_report_result_layout, id);

    this.context = context;
    this.id = id;

    if(name != null)
        this.name = name;
    if(total != null)
        this.total = total;
    if(transactionDate != null)
        this.transactionDate = transactionDate;
    if(transactionTime != null)
        this.transactionTime = transactionTime;
    if(transactionType != null)
        this.transactionType = transactionType;
}
Sign up to request clarification or add additional context in comments.

3 Comments

ive changed my code using your method but it still gives out the same error. do you think that it got to do with this line "View listViewMVRItem = inflater.inflate(R.layout.manager_view_report_result_layout, null, true);" or am i doing anything wrong when sending the start and end date using HashMap? anyway thank you very much for helping, im very new in android development, so just giving out ideas of where i might do wrong.
Try googling the error that you got and see if any of the situations described match yours.
Ive found out where i did wrong.. Feel like crying now... hahah. "CustomListManagerViewReportAdapter cl = new CustomListManagerViewReportAdapter(this, ManagerViewAllInventoryJsonWorker.id, ManagerViewReportJsonWorker.name," ManagerViewAllInventoryJsonWorker.id is supposed to be ManagerViewReportJsonWorker.id....... just 1 wrong name and i ended up sleeping so late.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.