第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么我的代碼運行出來報了空指針!

package com.immoc.collection;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Maptest {
?? ?
?? ?//用來橙裝學生類型的對象
?? ?
?? ?
?? ?public Map<String,Student>students;
?? ?
?? ???? public void testMap(){
?? ??? ??? ?
?? ??? ??? ?this.students = new HashMap<String,Student>();
?? ???? }
?? ???????? ?
?? ???? /**
?? ????? *
?? ????? * 測試添加:判斷學生ID是否被占用
?? ????? *
?? ????? *
?? ????? *
?? ????? * @param args
?? ????? * @return
?? ????? */
?? ??? ?
?? ???? public void testPut(){
?? ??? ??? ?//創(chuàng)建Scanner對象,用來獲取輸入的學生ID與姓名
?? ??? ??? ?Scanner console = new Scanner (System.in);
?? ??? ??? ?int i =0;
?? ??? ??? ?while(i<3){
?? ??? ??? ??? ?System.out.println("請輸入學生ID");
?? ??? ??? ??? ?String ID = console.next();
?? ??? ??? ??? ?/*判斷ID是否被占用*/
?? ??? ??? ??? ?Student st=students.get(ID);
?? ??? ??? ??? ?if( st == null ){
?? ??? ??? ??? ??? ?System.out.println("請輸學生姓名:");
?? ??? ??? ??? ??? ?String name = console.next();
?? ??? ??? ??? ??? ?//創(chuàng)建學生對象
?? ??? ??? ??? ??? ?Student newStudent = new Student (ID,name);
?? ??? ??? ??? ??? ?students.put(ID, newStudent);
?? ??? ??? ??? ??? ?System.out.println("成功添加學生:“+students。getID。name");
?? ??? ??? ??? ??? ?i++;?? ?
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?System.out.println("該學生ID已被占用");
?? ??? ??? ??? ??? ?continue;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?
?? ???? }
?? ??? ?
?? ???? public? void testKeySet(){
?? ??? ??? ?//通過keyset方法,返回Map所有鍵的SET集合
?? ??? ??? ?Set<String> keySet = students.keySet();
?? ??? ??? ?//便利ketSet,取得每一個鍵,在調(diào)用get取得每個鍵對應的Value
?? ??? ??? ?
?? ??? ??? ?System.out.println("總共有:"+students.size()+"個學生");
?? ??? ??? ?//取得學生的容量
?? ??? ??? ?for(String stuId:keySet){
?? ??? ??? ??? ?
?? ??? ??? ??? ?Student st = students.get(stuId);
?? ??? ??? ??? ?if(st!=null)
?? ??? ??? ??? ??? ?System.out.println("學生:"+st.name);
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?
?? ???? }

?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ?
?? ??? ?Maptest mt = new Maptest();
?? ??? ?mt.testPut();
?? ??? ?mt.testKeySet();
?? ??? ?
?? ??? ?
?? ??? ?
?? ?}

}

正在回答

3 回答

public void testMap(){
?? ??? ??? ?
?? ??? ??? ?this.students = new HashMap<String,Student>();
?? ???? }

把void去掉就可以了。

0 回復 有任何疑惑可以回復我~

? ??????public void testMap(){
?? ??? ??? ?
?? ??? ??? ?this.students = new HashMap<String,Student>();
?? ???? }
? ?這里你是想寫構造方法吧?

? ?你寫的這個方法沒有執(zhí)行,students沒有初始化。

? ?構造方法:

????????public Maptest(){
?? ??? ??? ?
?? ??? ??? ?this.students = new HashMap<String,Student>();
?? ???? }

? ?創(chuàng)建Maptest時,students就會初始化。
? ? ? ? ? ? ?

0 回復 有任何疑惑可以回復我~

http://img1.sycdn.imooc.com//55fa7ebf0001e33704920173.jpg求解一下

0 回復 有任何疑惑可以回復我~
#1

teddy_tsui

把testMap() 改成Maptest()
2015-09-17 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么我的代碼運行出來報了空指針!

我要回答 關注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號