数据库空指针异常错误Android

时间:2012-06-21 03:02:29

标签: android sqliteopenhelper

我正在玩这个多插槽服务器Java代码,代码以某种方式工作,我想在这里添加一个数据库所以我修改它遇到了这个奇怪的错误:

TryServerActivity.java

 public class TryServerActivity extends Activity {
  DataSource mDataSource;

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

    // instantiate DataSource as commented by reTs
    mDataSource = new DataSource(this);
    mDataSource.open();
    MultipleSocketServer.main();
  }

  @Override
  protected void onResume(){
    mDataSource.open();
    super.onResume();
    Log.d("Server", "onResume()");
  }

  @Override
  protected void onStop(){
    mDataSource.close();
    super.onStop();
    Log.d("Server", "onStop()");
  }

  @Override
  protected void onDestroy(){
    mDataSource.close();
    super.onDestroy();
    Log.d("Server","onDestroy");
  }

  @Override
  protected void onPause() {
    mDataSource.close();
    super.onPause();
    Log.d("Server", "onPause()");
  }
}

DataSource.java

public class DataSource implements DBConstants{

private SQLiteDatabase mSQLiteDatabase;
private DatabaseHelper mDatabaseHelper;

public DataSource (Context context){
    mDatabaseHelper = new DatabaseHelper(context);
}

public void open() throws SQLiteException {
    mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
}

public void close() {
    mDatabaseHelper.close();
}

public void addUser(String name, String role, String ipaddress, String vendor, String resolution, String media, String timestamp){
    ContentValues values = new ContentValues();
    values.put(NAME, name);
    values.put(ROLE, role);
    values.put(IP_ADDRESS, ipaddress);
    values.put(VENDOR_ID, vendor);
    values.put(RESOLUTION, resolution);
    values.put(MEDIA_SUBSYSTEM, media);
    values.put(TIMESTAMP, timestamp);
    mSQLiteDatabase.insert(DB_TABLENAME_USERS, null, values);

}
}

}

MultipleSocketServer.java

public class MultipleSocketServer implements Runnable {

   private Socket connection;
   private String TimeStamp;
   private int ID;

   DataSource mDataSource;

   public static void main() {
    int port = 19999;
    int count = 0;
    try{
        ServerSocket socket1 = new ServerSocket(port);
        System.out.println("MultipleSocketServer Initialized");
        while (true) {
            Socket connection = socket1.accept();
            Runnable runnable = new MultipleSocketServer(connection, ++count);
            Thread thread = new Thread(runnable);
            thread.start();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

MultipleSocketServer(Socket s, int i) {
    this.connection = s;
    this.ID = i;    
}

public void run() {
    try {
        BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
        InputStreamReader isr = new InputStreamReader(is);
        int character;
        StringBuffer process = new StringBuffer();

        while((character = isr.read()) != 13) {
            process.append((char)character);
        }
            System.out.println(process + " ID: " + ID);
        String details = process.toString();
        System.out.println("details: " + details);


        String name = "", role = "", ipaddress = "", vendor = "", resolution = "", media = "media", timestamp = "";

        String [] split = details.split(",");
        for(int i = 0; i < split.length; i++){
            Log.d("MULTIPLESOCKET", "split[" + i + "]: " + split[i].toString());

            switch (i){
                case 0: name = split[i].toString(); break;
                case 1: role = split[i].toString(); break;
                case 2: ipaddress = split[i].toString(); break;
                case 3: vendor = split[i].toString(); break;
                case 4: resolution = split[i].toString(); break;
                case 5: timestamp = split[i].toString(); break;
            }
        }

        Log.d("MULTIPLE", "name: " + name + " role: " + role + " ipadd: " + ipaddress);
        Log.d("MULTIPLE", "vendor: " + vendor + " resolution: " + resolution + " media: " + media + " time: " + timestamp);

        mDataSource.addUser(name, role, ipaddress, vendor, resolution, media, timestamp);

            try {
            Thread.sleep(10000);
        } catch (Exception e){
            System.out.println("thread");
        }

        TimeStamp = new java.util.Date().toString();
        String returnCode = "Server repsonded at "+ TimeStamp + (char) 13 + "hi";
        BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
        Log.d("SServer", "connect input: " + connection.getInputStream());
        Log.d("SServer", "connect close: " + connection.isClosed());
        OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
        osw.write(returnCode);
        osw.flush();

    } catch (Exception e) {
        System.out.println(e);
        System.out.println("run");
    } finally {
        try {
            connection.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

}

logcat的:

E/AndroidRuntime(12050): FATAL EXCEPTION: main
E/AndroidRuntime(12050): java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.tryserver/com.tryserver.TryServerActivity}: java.lang.NullPointerException
E/AndroidRuntime(12050):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime(12050):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(12050):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(12050):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(12050):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(12050):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(12050):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(12050):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(12050):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(12050):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(12050):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(12050):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(12050): Caused by: java.lang.NullPointerException
E/AndroidRuntime(12050):    at com.tryserver.TryServerActivity.onCreate(TryServerActivity.java:27)
E/AndroidRuntime(12050):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(12050):    at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime(12050):    ... 11 more
W/ActivityManager(  104):   Force finishing activity com.tryserver/.TryServerActivity
W/ActivityManager(  104):   Force finishing activity com.android.settings/.ManageApplications

另一个logcat:

I/System.out(13763): MultipleSocketServer Initialized
W/ActivityManager(  104): Launch timeout has expired, giving up wake lock!
W/ActivityManager(  104): Activity idle timeout for HistoryRecord{40661060 com.tryserver/.TryServerActivity}
D/dalvikvm( 1427): GC_EXPLICIT freed 8K, 54% free 2547K/5511K, external 716K/1038K, paused 17ms
 I/System.out(13763): ttttty,Teacher,192.168.1.138,Andy-tech,800x480, at Thu Jun 21 11:16:41 GMT+08:00 2012 ID: 1
 I/System.out(13763): details: ttttty,Teacher,192.168.1.138,Andy-tech,800x480, at Thu Jun 21 11:16:41 GMT+08:00 2012
 D/MULTIPLESOCKET(13763): split[0]: test
 D/MULTIPLESOCKET(13763): split[1]: Teacher
 D/MULTIPLESOCKET(13763): split[2]: 192.168.1.138
 D/MULTIPLESOCKET(13763): split[3]: Andy-tech
 D/MULTIPLESOCKET(13763): split[4]: 800x480
 D/MULTIPLESOCKET(13763): split[5]:  at Thu Jun 21 11:16:41 GMT+08:00 2012
 D/MULTIPLE(13763): name: ttttty role: Teacher ipadd: 192.168.1.138
 D/MULTIPLE(13763): vendor: Andy-tech resolution: 800x480 media: VNC time:  at Thu Jun 21 11:16:41 GMT+08:00 2012
 I/System.out(13763): java.lang.NullPointerException
 I/System.out(13763): run

0 个答案:

没有答案