2011-12-08 6 views
-1

可能性の重複:
I can't figure out what's wrong with my NullPointerException or why it even existsNullPointerExceptionエラーではなく、間違っているのかを知るのに十分な経験を積ん

彼らは何ですか?私のアプリケーションは、データベースヘルパーにオブジェクトを渡そうとするたびにクラッシュし続けます。

変数名は "e"です。私はトースト通知のオブジェクト表示のデータを持っていて、すべてうまくいけますが、データベースヘルパーに渡すときに何も得られません。私は2行目ごとにログを追加しましたが、メソッドが決して開始しないので、何も渡されません。

オブジェクトの値を別々の変数に追加して渡しました(ヘルパーメソッドを書き直しました)が、それでもクラッシュします。

私は非常に窓から私のラップトップを投げるに近いです、私を助けてください。

マイプレースホルダ検索クラス

package com.typhoon2099.mediacatalogue; 

//This class is used to query the details of the barcode scanned or entered 
//This in done in lieu of an open online database 
//If an online database is found this class can be replaced to utilise it 
public class SearchForBarcode{ 
//Initialise Variables 
public String barcode; 
public String mediatype; 
public String title; 
public String author; 

//Set variables based on the input barcode 
public SearchForBarcode(String inputBarcode){   
    barcode = inputBarcode; 
    //An if statement is used because switch case cannot be used for anything other than int 
    //And a 13 digit barcode is too big to fit in an int variable. 
    //(JDK 1.7 supports string based cases but Android does not support it) 
    if (barcode.equals("5014503113629")){ 
     mediatype = "DVD"; 
     title = "The Young Ones Series One"; 
     author = "Rik Mayall, Ade Edmondson, Nigel Planer, Christopher Ryan, Alexei Sayle"; 
    } 
    else if (barcode.equals("5014503151423")){ 
     mediatype = "DVD"; 
     title = "The Office: Complete Series One & Two"; 
     author = "Ricky Gervais, Martin Freeman, Mackenzie Crook, Lucy Davis"; 
    } 
    else if (barcode.equals("5050582261103")){ 
     mediatype = "DVD"; 
     title = "Shaun of the Dead"; 
     author = "Simon Pegg, Nick Frost, Lucy davis, Kate Ashfield, Dylan Moran, Bill Nighy, Penelope Wilton"; 
    } 
    else if (barcode.equals("5021290037243")){ 
     mediatype = "Game"; 
     title = "Batman: Arkham Asylum (PS3)"; 
     author = "Warner Brothers"; 
    } 
    else if (barcode.equals("5026555402323")){ 
     mediatype = "Game"; 
     title = "Bioshock 2 (PS3)"; 
     author = "2K Games"; 
    } 
    else if (barcode.equals("5021290046573")){ 
     mediatype = "Game"; 
     title = "Tomb Raider Trilogy HD (PS3)"; 
     author = "Crystal Dynamics"; 
    } 
    else if (barcode.equals("6025273901261")){ 
     mediatype = "CD"; 
     title = "Flesh Tone"; 
     author = "Kelis"; 
    } 
    else if (barcode.equals("724358480924")){ 
     mediatype = "CD"; 
     title = "Duran Duran"; 
     author = "Duran Duran"; 
    } 
    else if (barcode.equals("5099969605529")){ 
     mediatype = "CD"; 
     title = "Sounds of the Universe"; 
     author = "Depeche Mode"; 
    } 
    else if (barcode.equals("9781427802118")){ 
     mediatype = "Book"; 
     title = "Sgt Frog Vol. 13"; 
     author = "Mine Yoshizaki"; 
    } 
    else if (barcode.equals("978009940953")){ 
     mediatype = "Book"; 
     title = "Star Wars - The New Jedi Order: Vector Prime"; 
     author = "R. A. Salvatore"; 
    } 
    else{ 
     mediatype = "Not Found"; 
     title = "Not Found"; 
     author = "Not Found"; 
    } 
} 
} 

マイデータベースのヘルパークラス

package com.typhoon2099.mediacatalogue; 

import java.util.ArrayList; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DatabaseHelper { 
    private static final String DATABASE_NAME = "mediaDatabase"; 
private static final int DATABASE_VERSION = 2; 
private static final String TABLE_NAME = "table"; 

private OpenHelper mDbHelper; 
private SQLiteDatabase mDb; 
private SQLiteDatabase mDbr; 
private final Context mCtx; 

private static final String TAG = "UserDbAdapter"; 

private static class OpenHelper extends SQLiteOpenHelper{ 
    OpenHelper(Context context){ 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table "+TABLE_NAME+"" + 
       " (_id integer primary key," + 
       " mediatype text not null," + 
       " title text not null" + 
       " author text not null" + 
       " wishlist integer" + 
       ");"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 

} 
public DatabaseHelper(Context ctx) { 
    this.mCtx = ctx; 
} 
//This method is called from the controlling class when the open() call is used 
//it creates an instance of openHelper (detailed above) and sets up appropriate 
//connections to the database. 
public DatabaseHelper open() throws SQLException { 
    mDbHelper = new OpenHelper(mCtx); 
    mDb = mDbHelper.getWritableDatabase(); 
    mDbr = mDbHelper.getReadableDatabase(); 
    return this; 
} 
//used to close db connections 
public void close() { 
    mDbHelper.close(); 
} 
//When we wish to insert a record we can call this method, passing 
//in the appropriate parameters. The method then binds the 
//parameters to the insert command and then executes it 
//against the appropriate table in the mDB database instance 
public long addMedia(SearchForBarcode result) { 
    Log.d(TAG,"Line 1");   
    ContentValues initialValues = new ContentValues(); 
    Log.d(TAG,"Line 2");   
    initialValues.put("_id", result.barcode); 
    Log.d(TAG,"Line 3");   
    initialValues.put("mediatype", result.mediatype); 
    Log.d(TAG,"Line 4");   
    initialValues.put("title", result.title); 
    Log.d(TAG,"Line 5");   
    initialValues.put("author", result.author); 
    Log.d(TAG,"Line 6");   
    initialValues.put("wishlist", 0); 
    Log.d(TAG,"Line 7");   

    return mDb.insert(TABLE_NAME, null, initialValues); 
} 
//This function is used to delete an item from the database by passing through the barcode for the item. 
public void deleteMedia(String barcode) { 
    String args = "_id = "+barcode; 
    mDb.delete(TABLE_NAME, args, null); 
} 

public void UpdateWishlist(String barcode, String addOrRemove){ 
    ContentValues args = new ContentValues(); 
    if(addOrRemove.equals("add")){ 
     args.put("wishlist", "1"); 
    } 
    else{ 
     args.put("wishlist","0"); 
    } 
    mDb.update(TABLE_NAME, args, "barcode = ?", new String[]{barcode}); 
} 

//This function is used to select all the madia of a particular type for displaying in the tabs 
public ArrayList<String[]> selectMedia(String mediatype) { 
    ArrayList<String[]> results = new ArrayList<String[]>(); 
    int counter = 0; 
    Cursor cursor = this.mDbr.query(TABLE_NAME, new String[]{"_id", "title", "author", "rating"}, "mediatype = "+"?",new String[]{mediatype}, null, null, "title asc"); 
    if (cursor.moveToFirst()) { 
     do { 
      results.add(new String[3]); 
      results.get(counter)[0] = cursor.getString(0).toString(); 
      results.get(counter)[1] = cursor.getString(1).toString(); 
      results.get(counter)[2] = cursor.getString(2).toString(); 
      results.get(counter)[3] = cursor.getString(3).toString(); 
      counter++; 
     } while (cursor.moveToNext()); 
    } 
    if (cursor != null && !cursor.isClosed()) { 
     cursor.close(); 
    } 
    return results; 
} 
} 

私はそうのように一緒に2を使用します。

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); 
    if (scanResult != null) { 
     if (resultCode == 0){ 
      //If the user cancels the scan 
      Toast.makeText(getApplicationContext(),"You cancelled the scan", 3).show(); 
     } 
     else{ 
      String contents = intent.getStringExtra("SCAN_RESULT"); 
      String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); 
      if (format.equals("EAN_13") || format.equals("UPC_A")){ 
       //If the barcode scanned is of the correct type then pass the barcode into the search method to get the product details 
       SearchForBarcode result = new SearchForBarcode(contents); 
       Log.d(TAG,"Got from Database");   
       if(result.title.equals("Not Found")){ 
        Toast.makeText(getApplicationContext(), "Details Not Found", 5).show(); 
       } 
       else{ 
        if(database.addMedia(result)>=0){ 
         Toast.makeText(getApplicationContext(),result.title+" added to catalogue", 5).show();      
        } 
        else{ 
         Toast.makeText(getApplicationContext(), "The product is already in the database", 5).show(); 
        } 
       } 
      } 
      else{ 
       //If the barcode is not of the correct type then display a notification 
       Toast.makeText(getApplicationContext(),contents+" "+format, 3).show(); 
      } 
     } 
    } 
} 

ログの猫:

12-08 16:28:20.525: I/System.out(6862): Sending WAIT chunk 
12-08 16:28:20.525: W/ActivityThread(6862): Application com.typhoon2099.mediacatalogue is waiting for the debugger on port 8100... 
12-08 16:28:20.626: I/dalvikvm(6862): Debugger is active 
12-08 16:28:20.726: I/System.out(6862): Debugger has connected 
12-08 16:28:20.726: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:20.926: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:21.126: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:21.326: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:21.526: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:21.737: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:21.937: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:22.137: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:22.337: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:22.537: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:22.738: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:22.938: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:23.138: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:23.338: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:23.538: I/System.out(6862): waiting for debugger to settle... 
12-08 16:28:23.739: I/System.out(6862): debugger has settled (1439) 
12-08 16:28:24.089: D/dalvikvm(6862): GC_EXTERNAL_ALLOC freed 53K, 50% free 2726K/5379K, external 0K/0K, paused 31ms 
12-08 16:28:28.784: W/KeyCharacterMap(6862): Can't open keycharmap file 
12-08 16:28:28.784: W/KeyCharacterMap(6862): Error loading keycharmap file '/system/usr/keychars/atmel-touchscreen.kcm.bin'. hw.keyboards.131074.devname='atmel-touchscreen' 
12-08 16:28:28.784: I/KeyCharacterMap(6862): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
12-08 16:28:31.636: D/PhoneWindow(6862): couldn't save which view has focus because the focused view [email protected] has no id. 
12-08 16:28:33.718: W/IInputConnectionWrapper(6862): showStatusIcon on inactive InputConnection 
12-08 16:28:33.748: W/IInputConnectionWrapper(6862): InputConnection = [email protected], active client = false 
12-08 16:28:38.553: D/MainActivity(6862): Got from Database 
12-08 16:29:12.206: W/ActivityThread(7049): Application com.typhoon2099.mediacatalogue is waiting for the debugger on port 8100... 
12-08 16:29:12.216: I/System.out(7049): Sending WAIT chunk 
12-08 16:29:12.226: I/dalvikvm(7049): Debugger is active 
12-08 16:29:12.416: I/System.out(7049): Debugger has connected 
12-08 16:29:12.416: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:12.616: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:12.817: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:13.017: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:13.217: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:13.427: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:13.627: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:13.817: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:14.028: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:14.228: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:14.428: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:14.628: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:14.828: I/System.out(7049): waiting for debugger to settle... 
12-08 16:29:15.029: I/System.out(7049): debugger has settled (1343) 
12-08 16:29:15.269: D/dalvikvm(7049): GC_EXTERNAL_ALLOC freed 58K, 50% free 2726K/5379K, external 0K/0K, paused 30ms 
12-08 16:29:16.650: W/KeyCharacterMap(7049): Can't open keycharmap file 
12-08 16:29:16.650: W/KeyCharacterMap(7049): Error loading keycharmap file '/system/usr/keychars/atmel-touchscreen.kcm.bin'. hw.keyboards.131074.devname='atmel-touchscreen' 
12-08 16:29:16.650: I/KeyCharacterMap(7049): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
12-08 16:29:18.973: D/PhoneWindow(7049): couldn't save which view has focus because the focused view [email protected]f8 has no id. 
12-08 16:29:20.044: W/IInputConnectionWrapper(7049): showStatusIcon on inactive InputConnection 
12-08 16:29:20.044: W/IInputConnectionWrapper(7049): InputConnection = [email protected], active client = false 
12-08 16:29:25.249: D/MainActivity(7049): Got from Database 
12-08 16:29:31.895: D/AndroidRuntime(7049): Shutting down VM 
12-08 16:29:31.895: W/dalvikvm(7049): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) 
12-08 16:29:31.965: E/AndroidRuntime(7049): FATAL EXCEPTION: main 
12-08 16:29:31.965: E/AndroidRuntime(7049): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=195543262, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {com.typhoon2099.mediacatalogue/com.typhoon2099.mediacatalogue.MainActivity}: java.lang.NullPointerException 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2883) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:2925) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.app.ActivityThread.access$2000(ActivityThread.java:132) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1063) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.os.Looper.loop(Looper.java:143) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.app.ActivityThread.main(ActivityThread.java:4196) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at java.lang.reflect.Method.invoke(Method.java:507) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at dalvik.system.NativeStart.main(Native Method) 
12-08 16:29:31.965: E/AndroidRuntime(7049): Caused by: java.lang.NullPointerException 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at com.typhoon2099.mediacatalogue.MainActivity.onActivityResult(MainActivity.java:193) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.app.Activity.dispatchActivityResult(Activity.java:4010) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:123) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2879) 
12-08 16:29:31.965: E/AndroidRuntime(7049):  ... 11 more 
12-08 16:29:33.467: I/Process(7049): Sending signal. PID: 7049 SIG: 9 

+4

[私のNullPointerExceptionに何が問題なのか、それがなぜ存在するのかわかりません](http://stackoverflow.com/questions/8434094/i-cant-figure-out-whats-間違っていると私のnullpointerexception - または - それはなぜか - exis) – Craigy

+1

どの行はMainActivity.java:193ですか? –

+1

MainActivity.java:193... MainActivity.javaの193行目にコメントを書くことができます – havexz

答えて

1

の代わりに投げとして来ていないことを確認してくださいあなたのラップトップは、ウィンドウのうち、なぜunderstanding what a NullPointerException isで開始していない、探してhow to debug an Android application

例外の原因となっているコードの行を正確に特定するために、コードの実行を進めることができます。問題を見つけたら、原因を診断するのは簡単です。

+0

私はそれが 'database.addMedia(result);'行だと確信しています。それが動作する前にログが記録され、プログラムがクラッシュします。残念ながら私はどのように変数を間違って渡しているのか分かりません。私は必要なすべての情報をトーストしているので、オブジェクトは正常に表示されます。また、私のアプリは明日に手渡されなければなりません。 –

+0

1)あなたはそれらのリンクを読んだことがありますか? 2)完全なスタックトレースをここに投稿すると、現在のlogcat出力が切り捨てられ、NullPointerExceptionが発生する正確な行が見えなくなります。 – donturner

+0

12-08 17:36:07.698:D/MainActivity(7809):結果= [email protected] –

0

これはPROBあるように見える:

の代わりに:

String contents = intent.getStringExtra("SCAN_RESULT"); 
String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); 

試してみてください。

String contents = scanResult.getContents(); 
String format = scanResult.getFormatName(); 

contentsformatnull

+0

これはできません。私はToast通知を変数の内容と表示の罰金と共に掲示しました。 また、IntentIntegratorではなくIntentRerultのgetExtrasはありませんか? 同じ理由でクラッシュしました。 –

+0

ちょうど更新された答え..試しにこれを – havexz

+0

答えになりません、手動でバーコードを入力する場所の上に同じエラーが表示されます。私はそれが上記のデータベースヘルパーを宣言することと関係があると思います。それは私が考えることができるすべてです。 –

関連する問題