Android - NullPointerException: storage == null

Hi, I'm having problems with my android app. I am building a tutorial online. It keeps crashing when I try to start it with the specified exception. I would really appreciate some help on how to stop this error. Here's my main activity:

package com.niall.easytxt;

import android.app.Activity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

private ChatArrayAdapter adp;
private ListView list;
private EditText chatText;  
private Button send;
Intent in;
private boolean side = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = getIntent();
    send = (Button)findViewById(R.id.btn);
    list = (ListView)findViewById(R.id.listview);
    adp = new ChatArrayAdapter(getApplicationContext(),R.layout.chat, null);
    chatText = (EditText)findViewById(R.id.chat);
    chatText.setOnKeyListener(new OnKeyListener(){
        public boolean onKey(View v, int keyCode, KeyEvent event){
            if((event.getAction()==KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
            {
                return sendChatMessage();
            }


            return false;
        }






    });
    send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            sendChatMessage();
        }
    });

    list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    list.setAdapter(adp);
    adp.registerDataSetObserver(new DataSetObserver() {
        public void OnChanged(){
            super.onChanged();
            list.setSelection(adp.getCount() -1);
        }

    });


    }


private boolean sendChatMessage() {


    adp.add(new ChatMessage(side,chatText.getText().toString()));
    chatText.setText("");

    side =! side;


    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

      

And my ChatArrayAdapter:

package com.niall.easytxt;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChatArrayAdapter extends ArrayAdapter<ChatMessage>{
private TextView chatText;
private List<ChatMessage> MessageList = new ArrayList<ChatMessage>();
private LinearLayout layout;




public ChatArrayAdapter(Context context, int resource, ChatMessage[] objects) {
    super(context, resource, objects);

}

public void add(ChatMessage object) {

    MessageList.add(object);
    super.add(object);
}
public int getCount()
{
    return this.MessageList.size();
}
public ChatMessage getItem(int index)
{

    return this.MessageList.get(index);

}
public View getView(int position, View ConvertView, ViewGroup parent)
{
    View v = ConvertView;
    if(v==null){
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v =inflater.inflate(R.layout.chat, parent,false);
    }

    layout = (LinearLayout)v.findViewById(R.id.Message1);
    ChatMessage messageobj = getItem(position);
    chatText = (TextView)v.findViewById(R.id.SingleMessage);

    chatText.setText(messageobj.message);
    chatText.setBackgroundResource(messageobj.left ? R.drawable.bubble_a :R.drawable.bubble_b);
    layout.setGravity(messageobj.left?Gravity.START:Gravity.END);

    return v;

}
public Bitmap decodeToBitmap(byte[] decodedByte){
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

}

      

Here is logcat

04-29 05:40:03.988: D/AndroidRuntime(1794): Shutting down VM
04-29 05:40:03.988: E/AndroidRuntime(1794): FATAL EXCEPTION: main
04-29 05:40:03.988: E/AndroidRuntime(1794): Process: com.niall.easytxt, PID: 1794
04-29 05:40:03.988: E/AndroidRuntime(1794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.niall.easytxt/com.niall.easytxt.MainActivity}: java.lang.NullPointerException: storage == null
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.os.Looper.loop(Looper.java:135)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.app.ActivityThread.main(ActivityThread.java:5221)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invoke(Native Method)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invoke(Method.java:372)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-29 05:40:03.988: E/AndroidRuntime(1794): Caused by: java.lang.NullPointerException: storage == null
04-29 05:40:03.988: E/AndroidRuntime(1794):     at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at java.util.Arrays.asList(Arrays.java:155)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at com.niall.easytxt.ChatArrayAdapter.<init>(ChatArrayAdapter.java:26)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at com.niall.easytxt.MainActivity.onCreate(MainActivity.java:33)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.app.Activity.performCreate(Activity.java:5933)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-29 05:40:03.988: E/AndroidRuntime(1794):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-29 05:40:03.988: E/AndroidRuntime(1794):     ... 10 more

      

+3


source to share


2 answers


adp = new ChatArrayAdapter(getApplicationContext(),R.layout.chat, null);

      

Your problem lies on this line. You are passing null as dataset. Null link ChatMessage[]

. Super constructor calls

Arrays.asList(objects)

      



which throws an exception. To fix this, enter an empty dataset. For example, you can change your constructor like this:

public ChatArrayAdapter(Context context, int resource) {
    super(context, resource, MessageList);
}

      

+6


source


Inside onCreate in MainActivity

adp = new ChatArrayAdapter(getApplicationContext(),R.layout.chat, null);

      



The third argument to this call is the dataset from which the adapter is retrieving data. Passing null will cause it to throw a NullPointerException as soon as it tries to get data to it.

So, to solve this problem, just change this "null" to the actual ChatMessage array.

0


source







All Articles