android中使用字符串数组的空指针异常

时间:2015-07-13 09:06:32

标签: java android exception android-fragments nullpointerexception

我有一个名为Tab1的片段,我从中调用名为sendStatusTab1的方法并将结果存储在String数组中。 sendStatusTab1进一步调用MyDBHandler中的方法retrieveStatus,该方法也返回一个数组。

我在代码中看到了NullPointerException,并且进一步提供了错误。

我该如何纠正?

Tab1片段的代码:

package com.example.mrbrminocha.login; 

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageButton;
import android.widget.TextView;

import java.util.zip.Inflater;

public class Tab1 extends Fragment {
    ImageButton IB;
    TextView parking_left;
    String buttonID;
    String arrayStatus[] = new String[86];
    int booked_counter=0;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_1,container,false);

        arrayStatus = ((MainActivity2)this.getActivity()).sendStatusTab1(1);

        for (int i = 0; i < 86; i++) {
            buttonID = "button" + (i + 1);
            int resID = getResources().getIdentifier(buttonID, "id",  ((MainActivity2) this.getActivity()).getPackageName());
            IB = (ImageButton) v.findViewById(resID);
            if (arrayStatus[i].equals("Booked")) {
                booked_counter++;
                IB.setImageResource(R.drawable.grey1);
                IB.setClickable(false);
            }
        }

        parking_left = (TextView) v.findViewById(R.id.counter);
        parking_left.setText("Parking Left" + (86-booked_counter) + "/86");
        booked_counter = 0;
        return v;
    }
}

MainActivity2中显示错误的方法代码:

public String[] sendStatusTab1(int table) {
    arraystatus = dbHandler.retrieveStatus(arraystatus, table);
    return arraystatus;
}

MyDBHandler中方法的代码:

public String[] retrieveStatus(String array[],int table) {

    String table_name = "";
    switch (table) {
        case 1: table_name = TABLE1;
                break;
    }
    int index = 0;
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM" + TABLE1 + "WHERE 1";

    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();

    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex("buttonstatus")) != null) {
            array[index++] = c.getString(c.getColumnIndex("buttonstatus"));
        }
        c.moveToNext();
    }
    db.close();
    return array;
}

为什么我会NullPointerException,我该如何解决?

java.lang.NullPointerException: Attempt to invoke virtual method   'java.lang.String[]   com.example.mrbrminocha.login.MyDBHandler.retrieveStatus(java.lang.String[], int)' on a null object reference
    at com.example.mrbrminocha.login.MainActivity2.sendStatusTab1(MainActivity2.java:16 1)
    at com.example.mrbrminocha.login.Tab1.onCreateView(Tab1.java:33)

1 个答案:

答案 0 :(得分:1)

你的sql-query错了。你忘记了fromwhere之间的空格。 这是正确的查询:

String query = "SELECT * FROM " + TABLE1 + " WHERE 1";