Сбой AsyncTask из-за исключения нулевого указателя с автоматически сгенерированным идентификатором Database Room - PullRequest
0 голосов
/ 16 февраля 2019

Я пытаюсь создать приложение, которое берет 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 Room с кодовой меткой представления для приложения и все еще получаю эту ошибку.

Спасибо

1 Ответ

0 голосов
/ 16 февраля 2019

одна строка кода, которая определенно вызвала бы исключение нулевого указателя: здесь (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);
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...