如何解决Java.lang.NullPointerException:尝试调用虚方法?

时间:2015-09-18 10:34:36

标签: java android nullpointerexception

我正在尝试向上下文菜单添加操作,即编辑和删除。这些操作将编辑或删除数据库表中的条目。 当按下编辑或删除选项时,应该访问表“db_table”以检索对应于id主键的“朋友”行。 id使用“info.position”确定,dManager.retrieveFriend()用于检索行。

当...... dManager.retrieveFriend(String.valueOf(item.position));被称为我得到下面的错误,应用程序崩溃。

有人可以解释为什么我会收到以下错误。如果你需要更多的代码或信息,这是我第一次使用Stack溢出。

上下文菜单项“handler”:

 public boolean onContextItemSelected(MenuItem item)
{

    String editFriend = dManager.retrieveFriend(String.valueOf(info.position));
    FragmentManager manager = getFragmentManager();
    switch(item.getItemId())
    {
        case 0:
            Log.i("Friend contextual edit", "0");
            edit_Friend_Dialog edit_Friend_Dialog = new edit_Friend_Dialog();
            edit_Friend_Dialog.show(manager, "Edit Friend");

            Bundle bundle = new Bundle();
            bundle.putString("1", editFriend);
            edit_Friend_Dialog.setArguments(bundle);
            break;
        case 1:
            Log.i("Friend contextual del", "1");
            Log.i("Postion", String.valueOf(info.position));
            String id = (friendList.getItemAtPosition(info.position).toString());
            id = id.substring(0, id.indexOf(" "));
            id = id.replace(",", "");
            Log.i("id", id);
            dManager.removeRowFriend("2");
            friends = dManager.retrieveRowsFriends();
            break;


    }
    return true;
}

数据库管理员(dManager)retrieveFriend:

public String retrieveFriend(String id)
{
    String[] coloumns = new String[] {"id","age","gender", "name", "last_name", "address"};
    // calling elements    in an array
    cursor =  this.db.rawQuery("select * from " + DB_TABLE + " where id = " + id, null, null);
    if (cursor != null)
    {
        cursor.moveToFirst();
        String friend = "";
        friend = cursor.getString(1) + ", " + cursor.getString(2) + ", " + cursor.getString(3) + ", " + cursor.getString(4) + ", " + cursor.getString(5);
        return friend;
    }
    return null;
}

Menu1_fragment.java

    public class menu1_Fragment extends android.support.v4.app.Fragment {
    public ListView friendList;
    private String friends;
    public DatabaseManager dManager;
    public AdapterView.AdapterContextMenuInfo info;
    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        rootview = inflater.inflate(R.layout.menu1_layout, container, false);
        return rootview;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final DatabaseManager dManager = new DatabaseManager(getContext());
        setHasOptionsMenu(true);
        friendList = (ListView) getView().findViewById(R.id.friendsList);
        registerForContextMenu(friendList);

        //retrieve all records from table and store in string
        friends = dManager.retrieveRowsFriends();
        String friendsList[] = friends.split("\\r?\\n");

        friendList.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, friendsList));
        friendList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long code) {
                String id = (friendList.getItemAtPosition(position).toString());
                id = id.substring(0, id.indexOf(" "));
                id = id.replace(",", "");
                dManager.removeRowFriend(id);
            }
        });

    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        inflater.inflate(R.menu.activity_itemdetail, menu);
        setMenuVisibility(true);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View veiw, ContextMenu.ContextMenuInfo menuInfo)
    {
        if(veiw.getId() == R.id.friendsList)
        {
            info = (AdapterView.AdapterContextMenuInfo) menuInfo;
            //menu.setHeaderTitle(friendList[info.position]);
            String[] menuItems = getResources().getStringArray(R.array.menu);
            for(int i = 0; i< menuItems.length; i++)
            {
                menu.add(Menu.NONE, i, i, menuItems[i]);
            }
        }
    }
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        String editFriend = dManager.retrieveFriend(String.valueOf(info.position));
        FragmentManager manager = getFragmentManager();
        switch(item.getItemId())
        {
            case 0:
                Log.i("Friend contextual edit", "0");
                edit_Friend_Dialog edit_Friend_Dialog = new edit_Friend_Dialog();
                edit_Friend_Dialog.show(manager, "Edit Friend");

                Bundle bundle = new Bundle();
                bundle.putString("1", editFriend);
                edit_Friend_Dialog.setArguments(bundle);
                break;
            case 1:
                Log.i("Friend contextual del", "1");
                Log.i("Postion", String.valueOf(info.position));
                String id = (friendList.getItemAtPosition(info.position).toString());
                id = id.substring(0, id.indexOf(" "));
                id = id.replace(",", "");
                Log.i("id", id);
                dManager.removeRowFriend("2");
                friends = dManager.retrieveRowsFriends();
                break;


        }
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // handle item selection
        FragmentManager manager = getFragmentManager();
        switch (item.getItemId()) {
            case R.id.add_item:
                add_Friend_Dialog add_Friend_Dialog = new add_Friend_Dialog();
                add_Friend_Dialog.show(manager, "Add Friend");
                return true;
            case R.id.remove_friend:
                remove_Friend_Dialog remove_Friend_Dialog = new remove_Friend_Dialog();
                remove_Friend_Dialog.show(manager, "Remove Friend");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }
}

错误:

9-18 20:17:08.474 20803-20803 / com.example.mm.oggy E / AndroidRuntime:FATAL EXCEPTION:main     处理:com.example.mm.oggy,PID:20803      java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String com.example.mm.oggy.DatabaseManager.retrieveFriend(java.lang.String)'             在com.example.mm.oggy.menu1_Fragment.onContextItemSelected(menu1_Fragment.java:80)             在android.support.v4.app.Fragment.performContextItemSelected(Fragment.java:2085)             在android.support.v4.app.FragmentManagerImpl.dispatchContextItemSelected(FragmentManager.java:2122)             在android.support.v4.app.FragmentController.dispatchContextItemSelected(FragmentController.java:308)             在android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:334)             在android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)             在android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)             在com.android.internal.policy.impl.PhoneWindow $ DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:4701)             在com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)             在com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)             在com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)             在com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)             在com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)             在com.android.internal.app.AlertController $ AlertParams $ 3.onItemClick(AlertController.java:1082)             在android.widget.AdapterView.performItemClick(AdapterView.java:305)             在android.widget.AbsListView.performItemClick(AbsListView.java:1146)             在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3053)             在android.widget.AbsListView $ 3.run(AbsListView.java:3860)             在android.os.Handler.handleCallback(Handler.java:739)             在android.os.Handler.dispatchMessage(Handler.java:95)             在android.os.Looper.loop(Looper.java:135)             在android.app.ActivityThread.main(ActivityThread.java:5257)             at java.lang.reflect.Method.invoke(Native Method)             在java.lang.reflect.Method.invoke(Method.java:372)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:1)

dManager在异常中指定的行处为空。