由于Null指针异常以及Room Database自动生成的ID,AsyncTask崩溃

时间:2019-02-16 03:24:54

标签: android nullpointerexception android-room

我正在尝试创建一个带有QR码并将数据放入会议室数据库的应用。我当前在自动生成的类中收到空指针错误,应用程序崩溃。 这是错误堆栈跟踪

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.example.davin.scoutingapp2019, PID: 21281
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:318)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.davin.scoutingapp2019.Team.getId()' on a null object reference
        at com.example.davin.scoutingapp2019.TeamDAO_Impl$1.bind(TeamDAO_Impl.java:35)
        at com.example.davin.scoutingapp2019.TeamDAO_Impl$1.bind(TeamDAO_Impl.java:27)
        at android.arch.persistence.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:63)
        at com.example.davin.scoutingapp2019.TeamDAO_Impl.insert(TeamDAO_Impl.java:89)
        at com.example.davin.scoutingapp2019.TeamRepository$insertAsyncTask.doInBackground(TeamRepository.java:39)
        at com.example.davin.scoutingapp2019.TeamRepository$insertAsyncTask.doInBackground(TeamRepository.java:29)
        at android.os.AsyncTask$2.call(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
        at java.lang.Thread.run(Thread.java:761) 

这是实体团队

package com.example.davin.scoutingapp2019;

import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;


@Entity(tableName = "TeamInfo")
public class Team {
    @PrimaryKey(autoGenerate = true)
    private int id;

    @ColumnInfo(name = "team")
    private int teamNumber;

    public Team(int teamNumber, String position, boolean habLine, boolean sandstormCargoBalls, boolean sandstormCargoHatches, boolean sandstormRocketBalls, boolean sandstormRocketHatches, int CargoBallTotal, int CargoHatchesTotal, int RocketBallsTotal, int RocketHatchesTotal, String rocketRole, String climberRole, String overallRole, String otherComments) {
        this.teamNumber = teamNumber;
        this.position = position;
        this.habLine = habLine;
        this.sandstormCargoBalls = sandstormCargoBalls;
        this.sandstormCargoHatches = sandstormCargoHatches;
        this.sandstormRocketBalls = sandstormRocketBalls;
        this.sandstormRocketHatches = sandstormRocketHatches;
        this.CargoBallTotal = CargoBallTotal;
        this.CargoHatchesTotal = CargoHatchesTotal;
       this.RocketBallsTotal = RocketBallsTotal;
        this.RocketHatchesTotal = RocketHatchesTotal;
        this.rocketRole = rocketRole;
        this.climberRole = climberRole;
        this.overallRole = overallRole;
        this.otherComments = otherComments;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setTeamNumber(int teamNumber) {
        this.teamNumber = teamNumber;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public void setHabLine(boolean habLine) {
        this.habLine = habLine;
    }

    public void setSandstormCargoBalls(boolean sandstormCargoBalls) {
        this.sandstormCargoBalls = sandstormCargoBalls;
    }

    public void setSandstormCargoHatches(boolean sandstormCargoHatches) {
        this.sandstormCargoHatches = sandstormCargoHatches;
    }

    public void setSandstormRocketBalls(boolean sandstormRocketBalls) {
        this.sandstormRocketBalls = sandstormRocketBalls;
    }

    public void setSandstormRocketHatches(boolean sandstormRocketHatches) {
        this.sandstormRocketHatches = sandstormRocketHatches;
    }

    public void setCargoBallTotal(int cargoBallTotal) {
        CargoBallTotal = cargoBallTotal;
    }

    public void setCargoHatchesTotal(int cargoHatchesTotal) {
        CargoHatchesTotal = cargoHatchesTotal;
    }

    public void setRocketBallsTotal(int rocketBallsTotal) {
        RocketBallsTotal = rocketBallsTotal;
    }

    public void setRocketHatchesTotal(int rocketHatchesTotal) {
        RocketHatchesTotal = rocketHatchesTotal;
    }

    public void setRocketRole(String rocketRole) {
        this.rocketRole = rocketRole;
    }

    public void setClimberRole(String climberRole) {
        this.climberRole = climberRole;
    }

    public void setOverallRole(String overallRole) {
        this.overallRole = overallRole;
    }

    public void setOtherComments(String otherComments) {
        this.otherComments = otherComments;
    }

    @ColumnInfo(name="Position")
    private String position;
    @ColumnInfo(name="Crossed Hab Line")
    private boolean habLine;
    @ColumnInfo(name="Put Balls in Cargo Ship during Sandstorm")
    private boolean sandstormCargoBalls;
    @ColumnInfo(name="Put hatches in Cargo Ship during Sandstorm")
    private boolean sandstormCargoHatches;
    @ColumnInfo(name="Put Balls in Rocket Ship during Sandstorm")
    private boolean sandstormRocketBalls;
    @ColumnInfo(name="Put Hatches in Rocket Ship during Sandstorm")
    private boolean sandstormRocketHatches;


    @ColumnInfo(name="Total balls in Cargo Ship")

    private int CargoBallTotal;
    @ColumnInfo(name="Total hatches in Cargo Ship")
    private int CargoHatchesTotal;
    @ColumnInfo(name="Total balls in Rocket Ship")
    private int RocketBallsTotal;
    @ColumnInfo(name="Total hatches in Rocket Ship")
    private int RocketHatchesTotal;

    @ColumnInfo(name="Rocket Role")
    private String rocketRole;
    @ColumnInfo(name="Climbing Role")
    private String climberRole;
    @ColumnInfo(name="Overall Role")
    private String overallRole;
    @ColumnInfo(name="Additional Comments")
    private String otherComments;

    public int getId() {
        return id;
    }

    public int getTeamNumber() {
        return teamNumber;
    }

    public String getPosition() {
        return position;
    }

    public boolean isHabLine() {
        return habLine;
    }

    public boolean isSandstormCargoBalls() {
        return sandstormCargoBalls;
    }

    public boolean isSandstormCargoHatches() {
        return sandstormCargoHatches;
    }

    public boolean isSandstormRocketBalls() {
        return sandstormRocketBalls;
    }

    public boolean isSandstormRocketHatches() {
        return sandstormRocketHatches;
    }

    public int getCargoBallTotal() {
        return CargoBallTotal;
    }

    public int getCargoHatchesTotal() {
        return CargoHatchesTotal;
    }

    public int getRocketBallsTotal() {
        return RocketBallsTotal;
    }

    public int getRocketHatchesTotal() {
        return RocketHatchesTotal;
    }

    public String getRocketRole() {
        return rocketRole;
    }

    public String getClimberRole() {
        return climberRole;
    }

    public String getOverallRole() {
        return overallRole;
    }

    public String getOtherComments() {
        return otherComments;
    }
}

package com.example.davin.scoutingapp2019;

import android.arch.lifecycle.LiveData;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;

import java.util.List;

这是DAO

@Dao
public interface TeamDAO {


    @Insert
    void insert(Team t);


    @Query("Select * From TeamInfo ")
    LiveData<List<Team>> getAllData();

    @Query("Select * From TeamInfo Where team=:o")
    LiveData<List<Team>> getAllTeamData(int o);


    @Query("Select * From TeamInfo Order By `Total balls in Cargo Ship`")
    LiveData<List<Team>> mostCargoBalls();

    @Query("Select * From TeamInfo Order By `Total hatches in Cargo Ship`")
    LiveData<List<Team>> mostCargoHatches();

    @Query("Select * From TeamInfo Order By `Total balls in Rocket Ship`")
    LiveData<List<Team>> mostRocketBalls();


    @Query("Select * From TeamInfo Order By `Total hatches in Rocket Ship`")
    LiveData<List<Team>> mostRocketHatches();

}

这是Github上其余的代码 https://github.com/sejudav20/A-Spartan-Scouting-App-

我一直在使用该应用的视图代码实验室跟踪Google的会议室,但仍然出现此错误。

谢谢

1 个答案:

答案 0 :(得分:0)

肯定会导致空指针异常的一行代码是here(newTeam未分配任何内容):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    if(getIntent().getIntExtra("pass",0)==9){
        //Here; where is newTeam assigned? you're passing a null object to insert;
        teamViewModel.insert(newTeam);
    }