로그인 해주세요.


  • 목록
  • 아래로
  • 위로
  • 쓰기
  • 검색

잡담 자바 프로그래밍 잘하시는 분 있나요?

public class Dataset {
 public String query;
 public String answer;
 public int popularity;
 public double priority1;
 public double respTime;
 public double priority2;
 public double compTime;
 public double priority3;
 public double finalPriority;
 
 public Dataset(String query, String answer, int popularity, double priority1, double respTime, double priority2, double compTime, double priority3, double finalPriority) {
  this.query = query;
  this.answer = answer;
  this.popularity = popularity;
  this.priority1 = priority1;
  this.respTime = respTime;
  this.priority2 = priority2;
  this.compTime = compTime;
  this.priority3 = priority3;
  this.finalPriority = finalPriority;
 }
}

 

이렇게 클래스가 하나 있고

 

public class Cache {
 private final int bytesPerBlock;
 private final int blockSize;

 

 private final Dataset[] dataArray;

 

 public Cache(int blockSize, int bytesPerBlock) {
  this.bytesPerBlock = bytesPerBlock;
  this.blockSize = blockSize;

  dataArray = new Dataset[blockSize];

  for (int i = 0; i < blockSize; i++) {
   dataArray[i].query = "";
   dataArray[i].answer = "";
   dataArray[i].popularity = 0;
   dataArray[i].priority1 = 0.0;
   dataArray[i].respTime = 0.0;
   dataArray[i].priority2 = 0.0;
   dataArray[i].compTime = 0.0;
   dataArray[i].priority3 = 0.0;
   dataArray[i].finalPriority = 0.0;
  }

 }

 

이런 클래스가 있습니다. 이제 이 Cache 생성자를

 

public class memoryManager {
 private final Cache l1Cache;

 

 public memoryManager(int l1BS, int l1BP) {
  l1Cache = new Cache(l1BS, l1BP);
 }

 

여기서 호출하고 위의 생성자를

 

public class simulator {
 private final memoryManager m;

 

 public simulator(int l1BS, int l1BP) {
  m = new memoryManager(l1BS, l1BP);
 }

 

이렇게 호출하는데, 또 이 simulator 생성자는 밑의 메인함수에서 호출합니다.

 

 public static void main(String[] args) throws FileNotFoundException, IOException {
  System.out.println("Welcome to Cache Simulation");


  Scanner in = new Scanner(System.in);

 

  System.out.println("\nPlease specify if you would like to use l2 cache for the simulation (y/n)\n");

  String answer = in.nextLine();

  // if the user only wants to use L1 cache or L1 and L2 cache

  if (answer.substring(0, 1).equalsIgnoreCase("n")) {
   
   boolean flag = true;
   System.out.println(
     "\nNow enter the desired block size for l1 cache " + "and its corresponding bytes per block\n");

   String line1 = in.nextLine();
   String[] level1 = line1.split(" ");

   int blockSizeL1 = Integer.parseInt(level1[0]);
   int bytesPerBlockL1 = Integer.parseInt(level1[1]);

   simulator s = new simulator(blockSizeL1, bytesPerBlockL1);

 

이렇게 blockSize와 bytesPerBlock 값을 입력받은 후 그 값을 할당해 진행하는 방식입니다.

 

근데 제가 저 값을 입력하면 NullPointerException이 발생하더군요.

 

dataArray[i].query = "";

 

이 항에서 발생한다는데 이게 제가 보기엔 Dataset 생성자를 호출하지 않아 해당 클래스의 객체가 생성되지 않아 그런거 같은데 맞나요?

 

그러면 이렇게 클래스명을 자료형으로 쓰는 배열은 어떻게 초기화해야하죠 ㅠㅠㅠ

 

도와주세요...

추천인 1

공유

facebooktwitterpinterestbandkakao story
퍼머링크

댓글 8

1등 그래도아직은호아킨 2019.04.02. 22:43
아직 야근중이라 자바 전문은 아니지만 Dataset 배열을 크기만 정의하고
각 인덱스에 있는 클래스를 할당 안하시고 접근하셔서 그런것 같네요.
for문 돌때
dataArray[i] = new Dataset(); 하시고 해보시길...
아 이거 틀리면 창피한데 ㅠㅠ
댓글
profile image
title: #LIMGOHOMEVladimir 작성자 2019.04.03. 04:47
그래도아직은호아킨
이거로 일단은 해결했어요!! 감사합니다~
댓글
3등 title: 2018 WC 호드리구 모레노솔다도 2019.04.03. 00:36
저는 마지막에 이거 다 장난으로 한거라고 예상한;;
댓글
세프제이 2019.04.03. 03:29
이미 해결하셨을거 같지만..

컴파일 랭귀지는 시#만 알아서 틀릴수 있는데,
for loop 잘못 쓰신거 같네요.

dataArray = new Dataset[blockSize];

for (int i = 0; i < blockSize; i++) {
dataArray[i].query = "";
dataArray[i].answer = "";
dataArray[i].popularity = 0;
dataArray[i].priority1 = 0.0;
dataArray[i].respTime = 0.0;
dataArray[i].priority2 = 0.0;
dataArray[i].compTime = 0.0;
dataArray[i].priority3 = 0.0;
dataArray[i].finalPriority = 0.0;
}

이렇게 쓰시면 i = 0일때
dataArray[0].query = "";
dataArray[0].answer = "";
dataArray[0].popularity = 0;
dataArray[0].priority1 = 0.0;
dataArray[0].respTime = 0.0;
dataArray[0].priority2 = 0.0;
dataArray[0].compTime = 0.0;
dataArray[0].priority3 = 0.0;
dataArray[0].finalPriority = 0.0;

i = 1 일때
dataArray[1].query = "";
dataArray[1].answer = "";
dataArray[1].popularity = 0;
dataArray[1].priority1 = 0.0;
dataArray[1].respTime = 0.0;
dataArray[1].priority2 = 0.0;
dataArray[1].compTime = 0.0;
dataArray[1].priority3 = 0.0;
dataArray[1].finalPriority = 0.0;
...

이렇게 되요.

그럼 dataArray[0] = dataArray.query 니까
dataArray[0].query = dataArray.query.query 가 되서 널포인트 에러나는 걸 거에요.

개발자님들 화이팅 입니다.
댓글
MesSi 2019.04.03. 03:44
한국에선 한국말 썼으면 좋겠습니다.. 불편하네요..
댓글
profile image
KévinGameiro 2019.04.04. 03:32
근데 아뭉님들 능력있으시네요 다들...ㅋㅋㅋ
댓글
권한이 없습니다. 로그인

신고

"님의 댓글"

이 댓글을 신고 하시겠습니까?

삭제

"님의 댓글"

이 댓글을 삭제하시겠습니까?

번호 분류 제목 글쓴이 날짜 조회 수
662 잡담
image
title: #LIMGOHOME우다치 20.03.20.16:54 289 12
661 게임
image
title: 03/04 다비드 알벨다윤큐 20.03.21.09:57 233 8
660 잡담
image
title: #LIMGOHOME발렌시아종신! 20.03.21.12:47 287 5
659 잡담
image
title: #LIMGOHOME김박력 20.03.21.15:18 356 10
658 잡담
image
title: #LIMGOHOME림고홈 20.03.22.11:32 220 5
657 잡담
image
Hor01 20.03.22.13:24 204 4
656 잡담
image
title: #LIMGOHOME리빙스턴 20.03.23.09:31 283 7
655 잡담
image
title: #LIMGOHOMEregenta 20.03.24.00:46 270 3
654 게임
image
title: #LIMGOHOME아뭉박쥐 20.03.25.00:27 274 6
653 잡담
image
title: 06/07 다비드 비야블랙인스 20.03.25.17:31 229 7
652 게임
image
title: #LIMGOHOME아뭉박쥐 20.03.25.18:20 302 8
651 게임
image
title: #LIMGOHOMEv1oleT 20.03.26.18:11 907 3
650 잡담
image
title: 2010 WC 다비드 비야나랑하 20.03.28.18:08 352 11
649 잡담
image
title: #LIMGOHOME리빙스턴 20.03.30.14:49 264 8
648 게임
image
Amunt 20.03.30.17:23 407 10
647 잡담
image
RubenSobrino23 20.04.01.04:47 166 1
646 잡담
image
title: #LIMGOHOME아뭉박쥐 20.04.01.17:58 149 5
645 잡담
image
title: #LIMGOHOMEv1oleT 20.04.01.18:37 314 0
644 잡담
image
바티스투타 20.04.03.12:55 357 13
643 잡담
image
Aimery 20.04.03.14:50 237 11