Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - NullPointerException: storage == null

Tags:

java

android

Hi I'm having problems with an android app I'm building off of a tutorial online, It keeps crashing when I try to run it with the mentioned exception. I would really appreciate some help on how to stop this error please. 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's the 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
like image 609
Niall Avatar asked Apr 29 '15 09:04

Niall


People also ask

What is null pointer exception in Android?

java.lang.NullPointerException. Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object.


1 Answers

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

your problem lies on this line. You are passing null as dataset. A null reference of ChatMessage[]. The super constructor calls

Arrays.asList(objects)

which is causing the exception. To fix it provide an empty dataset. Eg you could change your constructor like:

public ChatArrayAdapter(Context context, int resource) {
    super(context, resource, MessageList);
}
like image 64
Blackbelt Avatar answered Oct 13 '22 17:10

Blackbelt