Trifit
Trifit

Reputation: 122

Stuck with NullPointerexception

I'm stuck. I'm tring to fill a listview with fields of a SQLite table. I'm quite new to programming Android. Any advice on why I am getting this error:

10-24 10:24:31.154: ERROR/AndroidRuntime(1298): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dvd.clase/com.dvd.clase.Lista_Classes}: java.lang.NullPointerException
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at android.os.Looper.loop(Looper.java:123)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at android.app.ActivityThread.main(ActivityThread.java:4595)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at java.lang.reflect.Method.invoke(Method.java:521)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at dalvik.system.NativeStart.main(Native Method)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): Caused by: java.lang.NullPointerException
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at com.dvd.clase.Lista_Classes.onCreate(Lista_Classes.java:72)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298):     ... 11 more

with this code (I think the problem is here and not in the SQL query, but I might be wrong):

public class Lista_Classes extends ListActivity{
BaseDatosHelper miBBDDHelper;
private class ClasseAdapter extends ArrayAdapter<Classe> {         
        ArrayList<Classe> items;

        public ClasseAdapter(Context context, int textViewResourceId, ArrayList<Classe> items) 
        {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.lista_item, null);
            }

            Classe classe = items.get(position);

            //if (classe != null) {
                TextView tnClasse = (TextView) v.findViewById(R.id.numClasse);
                TextView tColor = (TextView) v.findViewById(R.id.Color);
                if (tnClasse != null) {
                    tnClasse.setText(classe.GetNumClase());
                }
                if (tColor != null) {
                    tColor.setText(classe.GetColor());
                }
            //}
            return v;
        }
    }



    public void crearBBDD() {
        miBBDDHelper = new BaseDatosHelper(this);
        try {
            miBBDDHelper.crearDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        crearBBDD();

        ArrayList<Classe> classes = getItems();
        setListAdapter(new ClasseAdapter(this, R.layout.lista_item, classes));

        Button home= (Button) this.findViewById(R.id.boton_home);
        home.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view){
             Intent myIntent = new Intent(view.getContext(), main.class);
              startActivityForResult(myIntent, 0);
        }

    });
    }
    public ArrayList<Classe> getItems() {
        miBBDDHelper.abrirBaseDatos();
        ArrayList<Classe> listaClasses = miBBDDHelper.GetLlistaClasses();
        miBBDDHelper.close();
        return listaClasses;
    }
}

Thanks for your advice.

Upvotes: 1

Views: 234

Answers (2)

skynet
skynet

Reputation: 9908

You are calling findViewById but you never set the content view. You have to put setContentView(R.layout.yourlayout) in your onCreate method after you call super. This is why your home button is null.

Upvotes: 4

Alan Moore
Alan Moore

Reputation: 6575

According to your log, you should take a look at line 72 of your file, somewhere in your onCreate() -- I can't tell the line numbers from the code you put up. I would suggest single stepping through your onCreate using the debugger, that should reveal the problem pretty quickly. My guess is that your getItems() is returning null.

Upvotes: 0

Related Questions