通过从 Volley API 获取数据设置对象值后获取 NullPointerException
Getting NullPointerException after setting value of object by getting data from Volley API
从我的 API 获取信息后,我正在 parseJSON()
方法中设置 Team
对象的值。
即使变量 team 已被声明为静态并已初始化并且我在设置数据时使用 this
运算符,但数据未设置在 team
对象中并且当我执行 team.getTeamName() 我得到 java.lang.NullPointerException: println needs a message
代码:
public class TeamActivity extends AppCompatActivity {
private RequestQueue queue;
private String teamName;
private static Team team = new Team();
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_activity_layout);
Intent intent = getIntent();
teamName = intent.getStringExtra("teamName");
queue = Volley.newRequestQueue(this);
parseJSON();
Log.i("Team", this.team.getTeamName());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void parseJSON(){
String url = "http://192.168.0.174:8080/team/" + teamName;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, response -> {
try {
this.team.setId(Long.parseLong(response.getString("id")));
this.team.setTeamName(response.getString("teamName"));
this.team.setTotalMatches(Long.parseLong(response.getString("totalMatches")));
this.team.setTotalWins(Long.parseLong(response.getString("totalWins")));
}
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> Log.i("ERROR","Couldn't parse JSON"));
queue.add(jsonObjectRequest);
}}
java.lang.NullPointerException: println needs a message
错误意味着 team.getTeamName()
是 null
(日志正文不能是 null
)。这很明显,因为您在 QUEUING 请求 (parseJSON
) 之后调用 Log
。 Volley 会将这个请求排队,发送它,得到响应,然后用获取的数据(或错误)调用你的回调。这至少需要几毫秒,因为这是 IO 异步操作,同时您正在尝试 Log
清空数据
将您的 Log
调用 INTO 回调移动到您的 static Team
对象之后。然后它会工作并打印你的名字
this.team.setTeamName(response.getString("teamName"));
Log.i("Team", this.team.getTeamName()); // moved from onCreate
顺便说一句。 Team team
可能不应该设置为 static
甚至初始化。只要它是 null
你就会知道你的代码还没有获取数据。在回调中创建 tempTeam = new Team();
,解析数据并设置到这个本地实例。然后检查是否一切都被正确解析,如果是则将本地回调 Team
对象附加到 this.team = tempTeam;
之后你的 Log
将“开始工作”
private static Team team = new Team();
我假设构造函数正在为 teamName
初始化 null,能否请您在构造函数中将其更新为 empty
,以便 NullPointerException
可以避免或检查条件是否为空,并在不为空时记录它。
从我的 API 获取信息后,我正在 parseJSON()
方法中设置 Team
对象的值。
即使变量 team 已被声明为静态并已初始化并且我在设置数据时使用 this
运算符,但数据未设置在 team
对象中并且当我执行 team.getTeamName() 我得到 java.lang.NullPointerException: println needs a message
代码:
public class TeamActivity extends AppCompatActivity {
private RequestQueue queue;
private String teamName;
private static Team team = new Team();
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_activity_layout);
Intent intent = getIntent();
teamName = intent.getStringExtra("teamName");
queue = Volley.newRequestQueue(this);
parseJSON();
Log.i("Team", this.team.getTeamName());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void parseJSON(){
String url = "http://192.168.0.174:8080/team/" + teamName;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, response -> {
try {
this.team.setId(Long.parseLong(response.getString("id")));
this.team.setTeamName(response.getString("teamName"));
this.team.setTotalMatches(Long.parseLong(response.getString("totalMatches")));
this.team.setTotalWins(Long.parseLong(response.getString("totalWins")));
}
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> Log.i("ERROR","Couldn't parse JSON"));
queue.add(jsonObjectRequest);
}}
java.lang.NullPointerException: println needs a message
错误意味着 team.getTeamName()
是 null
(日志正文不能是 null
)。这很明显,因为您在 QUEUING 请求 (parseJSON
) 之后调用 Log
。 Volley 会将这个请求排队,发送它,得到响应,然后用获取的数据(或错误)调用你的回调。这至少需要几毫秒,因为这是 IO 异步操作,同时您正在尝试 Log
清空数据
将您的 Log
调用 INTO 回调移动到您的 static Team
对象之后。然后它会工作并打印你的名字
this.team.setTeamName(response.getString("teamName"));
Log.i("Team", this.team.getTeamName()); // moved from onCreate
顺便说一句。 Team team
可能不应该设置为 static
甚至初始化。只要它是 null
你就会知道你的代码还没有获取数据。在回调中创建 tempTeam = new Team();
,解析数据并设置到这个本地实例。然后检查是否一切都被正确解析,如果是则将本地回调 Team
对象附加到 this.team = tempTeam;
之后你的 Log
将“开始工作”
private static Team team = new Team();
我假设构造函数正在为 teamName
初始化 null,能否请您在构造函数中将其更新为 empty
,以便 NullPointerException
可以避免或检查条件是否为空,并在不为空时记录它。