2016-04-20 9 views
-3

私はアンドロイドの開発に慣れています。私はデータベースを作成し、 。 他のコードが必要な場合はお知らせください。ここで私のデータベースからデータを取得しようとしていますが、 "NULLポインタ例外"が発生しました

は私mainActivityです:

public static final String LOGTAG = "EMPLOYEES"; 

public static final String Name = "name"; 
public static final String Pic = "pic"; 
public static final String Position = "position"; 
public static final String Qualification = "qualification"; 
public static final String Expertise = "expertise"; 
public static final String Contact = "contact"; 


public static final int DETAIL_REQUEST_CODE = 1001; 
protected List<TeamDetails> data; 

private SharedPreferences settings; 
private SharedPreferences.OnSharedPreferenceChangeListener listener; 

EmployeeDataSource datasource; 

private GoogleApiClient client; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    datasource = new EmployeeDataSource(this); 
    datasource.open(); 

    List<TeamDetails> teamDetails = datasource.findAll(); 
    if (teamDetails.size() == 0) { 
     createData(); 
     teamDetails = datasource.findAll(); 
    } 


    ArrayAdapter<TeamDetails> courseArrayAdapter = 
      new nameArrayAdapter(this, 0, data); 
    ListView listView = (ListView) findViewById(R.id.list); 
    listView.setAdapter(courseArrayAdapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      TeamDetails teamDetails = data.get(position); 
      displayDetail(teamDetails); 
     } 
    }); 

    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    datasource.open(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    datasource.close(); 
} 

private void createData() { 

    TeamDetails teamDetails = new TeamDetails(); 
    teamDetails.setName("M Jaleed"); 
    teamDetails.setPosition("Team Lead"); 
    teamDetails.setQualities("Masters"); 
    teamDetails.setExpertise("Android Development"); 
    teamDetails.setContact(03331234567); 
    teamDetails.setImage(""); 
    teamDetails = datasource.create(teamDetails); 
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId()); 

    teamDetails = new TeamDetails(); 
    teamDetails.setName("Sajawal Nawaz"); 
    teamDetails.setPosition("Internee"); 
    teamDetails.setQualities("Bachelors"); 
    teamDetails.setExpertise("Android Development"); 
    teamDetails.setContact(03331234567); 
    teamDetails.setImage(""); 
    teamDetails = datasource.create(teamDetails); 
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId()); 

    teamDetails = new TeamDetails(); 
    teamDetails.setName("Waqas Khan"); 
    teamDetails.setPosition("Internee"); 
    teamDetails.setQualities("Bachelors"); 
    teamDetails.setExpertise("Android Development"); 
    teamDetails.setContact(03331234567); 
    teamDetails.setImage(""); 
    teamDetails = datasource.create(teamDetails); 
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId()); 

    teamDetails = new TeamDetails(); 
    teamDetails.setName("Waqas Khan Swat"); 
    teamDetails.setPosition("Internee"); 
    teamDetails.setQualities("Bachelors"); 
    teamDetails.setExpertise("Android Development"); 
    teamDetails.setContact(03331234567); 
    teamDetails.setImage(""); 
    teamDetails = datasource.create(teamDetails); 
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId()); 

    teamDetails = new TeamDetails(); 
    teamDetails.setName("Arslan Shah"); 
    teamDetails.setPosition("Internee"); 
    teamDetails.setQualities("Bachelors"); 
    teamDetails.setExpertise("Android Development"); 
    teamDetails.setContact(03331234567); 
    teamDetails.setImage(""); 
    teamDetails = datasource.create(teamDetails); 
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId()); 


} 


private void displayDetail(TeamDetails teamDetails) { 
    Intent intent = new Intent(this, DetailActivity.class); 
    intent.putExtra(Name, teamDetails.getName()); 
    intent.putExtra(Pic, teamDetails.getImage()); 
    intent.putExtra(Position, teamDetails.getPosition()); 
    intent.putExtra(Qualification, teamDetails.getQualities()); 
    intent.putExtra(Expertise, teamDetails.getExpertise()); 
    intent.putExtra(Contact, teamDetails.getContact()); 
    startActivityForResult(intent, DETAIL_REQUEST_CODE); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onStart() { 
    super.onStart(); 

    client.connect(); 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, 
      "Main Page", 
      Uri.parse("http://host/path"), 

      Uri.parse("android-app://com.example.sj.dgapps3/http/host/path") 
    ); 
    AppIndex.AppIndexApi.start(client, viewAction); 
} 

@Override 
public void onStop() { 
    super.onStop(); 

    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, 
      "Main Page", 
    Uri.parse("http://host/path"), 

      Uri.parse("android-app://com.example.sj.dgapps3/http/host/path") 
    ); 
    AppIndex.AppIndexApi.end(client, viewAction); 
    client.disconnect(); 
} 


class nameArrayAdapter extends ArrayAdapter<TeamDetails> { 

    Context context; 
    List<TeamDetails> objects; 

    public nameArrayAdapter(Context context, int resource, List<TeamDetails> objects) { 
     super(context, resource, objects); 

     this.context = context; 
     this.objects = objects; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     TeamDetails teamDetails = objects.get(position); 

     LayoutInflater inflater = 
       (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

     View view = inflater.inflate(R.layout.list, null); 

     TextView tv = (TextView) view.findViewById(R.id.name); 
     tv.setText(teamDetails.getName()); 

     ImageView iv = (ImageView) view.findViewById(R.id.empPic); 
     int res = context.getResources().getIdentifier(teamDetails.getImage(), "drawable", context.getPackageName() 
     ); 
     iv.setImageResource(res); 

     return view; 
    } 

} 

あなたは十分なスタックトレースが近くに読めばここでここでエラーログ

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.sj.dgapps3, PID: 1641 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sj.dgapps3/com.example.sj.dgapps3.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337) 
at android.widget.ListView.setAdapter(ListView.java:491) 
at com.example.sj.dgapps3.MainActivity.onCreate(MainActivity.java:104) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

あなたは、私のデータソースクラス

package DgApps3.db; 

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

import com.example.sj.dgapps3.TeamDetails; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by SJ on 4/14/2016. 
*/ 
public class EmployeeDataSource { 

    public static final String LOGTAG = "EMPLOYEES"; 

    SQLiteOpenHelper dbhelper; 
    SQLiteDatabase database; 

    private static final String[] allColumns = { 
      DgApps3DBOpenHelper.COLUMN_ID, 
      DgApps3DBOpenHelper.COLUMN_NAME, 
      DgApps3DBOpenHelper.COLUMN_POSITION, 
      DgApps3DBOpenHelper.COLUMN_PIC, 
      DgApps3DBOpenHelper.COLUMN_EXPERTISE, 
      DgApps3DBOpenHelper.COLUMN_QUALIFICATION, 
      DgApps3DBOpenHelper.COLUMN_CONTACT 
    }; 

    public EmployeeDataSource(Context context) { 
     dbhelper = new DgApps3DBOpenHelper(context); 
     //database = dbhelper.getWritableDatabase(); 
    } 

    public void open() { 
     Log.i(LOGTAG, "Database opened"); 
     database = dbhelper.getWritableDatabase(); 
    } 

    public void close() { 
     Log.i(LOGTAG, "Database closed"); 
     dbhelper.close(); 
    } 

    public TeamDetails create(TeamDetails teamDetails) { 
     ContentValues values = new ContentValues(); 
     values.put(DgApps3DBOpenHelper.COLUMN_NAME, teamDetails.getName()); 
     values.put(DgApps3DBOpenHelper.COLUMN_POSITION, teamDetails.getPosition()); 
     values.put(DgApps3DBOpenHelper.COLUMN_PIC, teamDetails.getImage()); 
     values.put(DgApps3DBOpenHelper.COLUMN_QUALIFICATION, teamDetails.getQualities()); 
     values.put(DgApps3DBOpenHelper.COLUMN_EXPERTISE, teamDetails.getExpertise()); 
     values.put(DgApps3DBOpenHelper.COLUMN_CONTACT, teamDetails.getContact()); 
     long insertid = database.insert(DgApps3DBOpenHelper.TABLE_EMPLOYEES, null, values); 
     teamDetails.setId(insertid); 
     return teamDetails; 
    } 

    public List<TeamDetails> findAll() { 
     List<TeamDetails> teamDetails = new ArrayList<TeamDetails>(); 

     Cursor cursor = database.query(DgApps3DBOpenHelper.TABLE_EMPLOYEES, allColumns, 
       null, null, null, null, null); 

     Log.i(LOGTAG, "Returned" + cursor.getCount() + " rows "); 
     if (cursor.getCount() > 0) { 
      while (cursor.moveToNext()) { 
       TeamDetails teamDetail = new TeamDetails(); 
       teamDetail.setId(cursor.getLong(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_ID))); 
       teamDetail.setName(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_NAME))); 
       teamDetail.setPosition(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_POSITION))); 
       teamDetail.setImage(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_PIC))); 
       teamDetail.setQualities(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_QUALIFICATION))); 
       teamDetail.setExpertise(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_EXPERTISE))); 
       teamDetail.setContact(cursor.getDouble(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_CONTACT))); 
       teamDetails.add(teamDetail); 
      } 

     } 
     return teamDetails; 
    } 
} 
+0

リスト data = newリスト(); – sushildlh

+0

@Sushilリスト data = new ArrayList ()リストはインターフェイス –

+0

です。彼は "List "にメモリを割り当てませんでした。なぜ "ヌルポインタ"が来るのか。 – sushildlh

答えて

0

ですリスト上のsizeメソッドを呼び出すときにエラーが発生することがわかります。あなたは、あなたは決してsizeメソッドを呼び出すと言うかもしれません。確かに、しかし、Androidのは...あなたはそのリストを、このコード

protected List<TeamDetails> data; 

ルックを初期化するのでしょうか?いいえ、それはヌルです。だから、どうやってこれがうまくいくと思いますか?

ArrayAdapter<TeamDetails> courseArrayAdapter = 
     new nameArrayAdapter(this, 0, data); 

これはエラーではありません。これを修正するには、アダプターの適切なリストを使用するか、アダプターを入れる前にアダプターを初期化してください。

+0

私はすでに初期化していますが、このコード行に問題があります listView.setAdapter (courseArrayAdapter); もし私がこれをコメントすれば、エラーはなくなり、私のリストも消えます。 –

+0

'data'変数がアダプタ内部でヌルなので、問題はその行にあります。そして、あなたがその行を実行するまで、あなたにエラーを通知することはありません。コードを変更した場合は、質問とその変更を反映するためにエラーを編集してください。 –

+0

これで解決しましたが、次の問題は画像をリストに渡すことができないことです。 –

関連する問題