Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

java.lang.class - java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException: storage == null

*I am trying to add items to the list view dynamically. But, I am getting a run time exception.Please help. Is there a problem with the global variable "values"? I tried to debug by printing the logs and I am able to see the values stored in the variable *

public class FriendsActivity extends ListFragment implements TabListener {
      private Fragment mFragment;
      private String[] values;

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         getActivity().setContentView(R.layout.activity_friends);
    }

@SuppressWarnings("deprecation")
public void onActivityCreated(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
  super.onActivityCreated(savedInstanceState);
  String su = User.getLoggedInUsername();
  friends.query(friends.class, new StackMobQuery().field(new StackMobQueryField("uname").isEqualTo(su)), new StackMobQueryCallback<friends>(){

    @Override
    public void failure(StackMobException arg0) {
        // TODO Auto-generated method stub

    }

    public void success(List<friends> arg0) {
        // TODO Auto-generated method stub
        if(arg0.size()>0){
            FriendsActivity.this.values = new String[arg0.size()];
            for(int i=0;i<arg0.size();i++){
                friends us = arg0.get(i);
                values[i] = us.getUname();
            }
            for(int i=0;i<values.length;i++)
                Log.i("Friends",values[i]);
        }
            else{
                values = new String[]{"xyz","abc","dbz"};

            }
        }

  });
  //String uname=User.getLoggedInUsername();
  //Log.i("User Name",uname);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
          android.R.layout.simple_list_item_1, values);
      setListAdapter(adapter);
}

Here's the error list

05-27 16:42:03.667: E/Trace(814): error opening trace file: No such file or directory     (2)
05-27 16:46:17.718: E/AndroidRuntime(814): FATAL EXCEPTION: main
05-27 16:46:17.718: E/AndroidRuntime(814): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.myapp.cloudchat/com.myapp.cloudchat.HomeScreenActivity}: java.lang.NullPointerException: storage == null
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.os.Looper.loop(Looper.java:137)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-27 16:46:17.718: E/AndroidRuntime(814):  at java.lang.reflect.Method.invokeNative(Native Method)
05-27 16:46:17.718: E/AndroidRuntime(814):  at java.lang.reflect.Method.invoke(Method.java:511)
05-27 16:46:17.718: E/AndroidRuntime(814):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-27 16:46:17.718: E/AndroidRuntime(814):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-27 16:46:17.718: E/AndroidRuntime(814):  at dalvik.system.NativeStart.main(Native Method)
05-27 16:46:17.718: E/AndroidRuntime(814): Caused by: java.lang.NullPointerException: storage == null
05-27 16:46:17.718: E/AndroidRuntime(814):  at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
05-27 16:46:17.718: E/AndroidRuntime(814):  at java.util.Arrays.asList(Arrays.java:154)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
05-27 16:46:17.718: E/AndroidRuntime(814):  at com.myapp.cloudchat.FriendsActivity.onActivityCreated(FriendsActivity.java:74)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.Fragment.performActivityCreated(Fragment.java:1703)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
05-27 16:46:17.718: E/AndroidRuntime(814):  at android.app.Activity.performStart(Activity.java:5113)
05-27 16:46:17.718: E/AndroidRuntime(814):  at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
05-27 16:46:17.718: E/AndroidRuntime(814):  ... 11 more
05-27 16:46:20.428: E/Trace(866): error opening trace file: No such file or directory (2)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This particular exception ("storage == null") is thrown when the array parameter passed to ArrayAdapter(Context, int, T[]) is null. (To be more specific, it's thrown from Arrays.java when it tries to create a new ArrayList wrapped around your null array.)

There should be no problem with the variable values itself, but are you absolutely sure that values is not null at the point you're passing it into the ArrayAdapter constructor? Just below the line

  //Log.i("User Name",uname);

and just above the constructor call, add something like this:

  Log.i("FriendsActivity", "values = <<" + values + ">>");

and see if it really is what it's supposed to be.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...