Bert
Bert

Reputation: 17

Android eclipse nullpointexceptionerror at runtime

I keep getting a Nullpointexceptionerror at runtime. According to logcat the error is caused by line 43 which is:

Database databaseNieuw = new Database(new File(naamDatabase), Version.DBASE_4); 

The only thing in that line which could throw such an error would be naamDatabase which is a String aquired from an EditText field. Which are both defined above line 43.

Any help would be greatly appreciated.

Logcat:

01-15 23:44:40.292: E/Trace(20437): error opening trace file: No such file or directory     (2)
01-15 23:44:46.769: E/AndroidRuntime(20437): FATAL EXCEPTION: main
01-15 23:44:46.769: E/AndroidRuntime(20437): java.lang.NullPointerException
01-15 23:44:46.769: E/AndroidRuntime(20437):    at nl.knaw.dans.common.dbflib.Database.<init>(Database.java:112)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at nl.knaw.dans.common.dbflib.Database.<init>(Database.java:64)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at com.example.bosbouwapp.screen1$2.onClick(screen1.java:43)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at android.view.View.performClick(View.java:4102)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at android.view.View$PerformClick.run(View.java:17085)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at android.os.Handler.handleCallback(Handler.java:615)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at android.os.Looper.loop(Looper.java:155)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at android.app.ActivityThread.main(ActivityThread.java:5454)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at java.lang.reflect.Method.invokeNative(Native Method)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at java.lang.reflect.Method.invoke(Method.java:511)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
01-15 23:44:46.769: E/AndroidRuntime(20437):    at dalvik.system.NativeStart.main(Native Method)

Code:

package com.example.bosbouwapp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import nl.knaw.dans.common.dbflib.Database;
import nl.knaw.dans.common.dbflib.Field;
import nl.knaw.dans.common.dbflib.InvalidFieldLengthException;
import nl.knaw.dans.common.dbflib.InvalidFieldTypeException;
import nl.knaw.dans.common.dbflib.Type;
import nl.knaw.dans.common.dbflib.Version;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class screen1 extends Activity {

    /** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen1);

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

    });

    Button next = (Button) findViewById(R.id.button3);
     next.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {

                EditText databaseNaam = (EditText)    findViewById(R.id.database_naam);
                String naamDatabase = databaseNaam.getText().toString();
                Database databaseNieuw = new Database(new File(naamDatabase), Version.DBASE_4);            
                Global g = (Global) getApplication();
                List<Field> fields = new ArrayList<Field>();
                List<Field> fields2 = new ArrayList<Field>();

      if (naamDatabase.trim().equals("")) {
        Toast.makeText(getApplication(), "Veld is leeg", Toast.LENGTH_SHORT).show();
      }  
      else{

    fields.add(new Field("Stapel", Type.NUMBER, 3));
    fields.add(new Field("Boomsoort", Type.CHARACTER, 25));
    fields.add(new Field("Diameter", Type.NUMBER, 4));
    fields.add(new Field("Lengte", Type.NUMBER, 2));
    fields.add(new Field("Overig", Type.CHARACTER, 150));
    try {
        databaseNieuw.addTable("Boomstam", fields);
    } catch (InvalidFieldTypeException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (InvalidFieldLengthException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    fields2.add(new Field("Stapel", Type.CHARACTER, 50));
    fields2.add(new Field("Datum", Type.DATE));
    fields2.add(new Field("X", Type.NUMBER, 20));
    fields2.add(new Field("Y", Type.NUMBER, 20));
    fields2.add(new Field("Houtsoort", Type.CHARACTER, 50));


    try {
        databaseNieuw.addTable("Houtstapel", fields2);
    } catch (InvalidFieldTypeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidFieldLengthException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    g.setDatabaseNieuw(databaseNieuw);      
    Intent myIntent = new Intent(view.getContext(), screen3.class);
            startActivityForResult(myIntent, 0);


        }

    }


    });
}
}  

EDIT

I'm currently looking into the Database.class. As I can't really make heads or tails out of it (yet), maybe someone else can.

Code of Database.class:

// Decompiled by DJ v3.12.12.96 Copyright 2011 Atanas Neshkov  Date: 1/16/2013 12:48:01 AM
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Database.java

package nl.knaw.dans.common.dbflib;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;

// Referenced classes of package nl.knaw.dans.common.dbflib:
//            Table, Version, InvalidFieldTypeException, InvalidFieldLengthException

public class Database
{

public Database(File databaseDirectory, Version version)
{
    this(databaseDirectory, version, Charset.defaultCharset().name());
}

public Database(File databaseDirectory, Version version, String charsetName)
{
    tableMap = new HashMap();
    if(databaseDirectory == null || databaseDirectory.isFile())
        throw new IllegalArgumentException("Database must be a directory ");
    if(!databaseDirectory.exists())
        databaseDirectory.mkdirs();
    this.databaseDirectory = databaseDirectory;
    this.version = version;
    this.charsetName = charsetName != null ? charsetName : Charset.defaultCharset().name();
    Charset.forName(this.charsetName);
    String fileNames[] = databaseDirectory.list();
    String arr$[] = fileNames;
    int len$ = arr$.length;
    for(int i$ = 0; i$ < len$; i$++)
    {
        String fileName = arr$[i$];
        if(fileName.toLowerCase().endsWith(".dbf") && fileName.length() > ".dbf".length())
            addTable(fileName);
    }

}

public Set getTableNames()
{
    return Collections.unmodifiableSet(tableMap.keySet());
}

public Table getTable(String name)
{
    return (Table)tableMap.get(name);
}

public Table addTable(String name, List fields)
    throws InvalidFieldTypeException, InvalidFieldLengthException
{
    Table table = (Table)tableMap.get(name);
    if(table == null)
    {
        table = new Table(new File(databaseDirectory, name), version, fields);
        tableMap.put(name, table);
    }
    return table;
}

private void addTable(String name)
{
    Table table = (Table)tableMap.get(name);
    if(table == null)
    {
        table = new Table(new File(databaseDirectory, name), charsetName);
        tableMap.put(name, table);
    }
}

public void removeTable(String name)
{
    tableMap.remove(name);
}

public void removeTable(Table table)
{
    tableMap.remove(table.getName());
}

public String getCharsetName()
{
    return charsetName;
}

private final File databaseDirectory;
private final Map tableMap;
private final Version version;
private final String charsetName;
}

Upvotes: 2

Views: 155

Answers (2)

Matthew
Matthew

Reputation: 9949

Can you put a break point in your code and step through it to see if you are trying to make a call at any point on a non-initialized object, or are passing a null object to the Database class? Specifically confirm that naamDatabase is not null when you pass it on line 43? Also is this string what you expect it to be at the point the call is made?

UPDATE

as I note in the comment, when stepping through what is the state of the file object below:

File file = new File(naamDatabase);

Breakpoint here - check file object is not null

Database databaseNieuw = new Database(file, Version.DBASE_4);

Upvotes: 1

Eurig Jones
Eurig Jones

Reputation: 8543

The error here is in the Database class, and without seeing that it's not possible to trace the problem. If you're sure that everything you're passing into Database is not null, then it's the Database class that is to blame.

I'd suggest getting in touch with whoever created that Database class, whatever it does!

Upvotes: 0

Related Questions