로그인 해주세요.


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

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

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
근데 아뭉님들 능력있으시네요 다들...ㅋㅋㅋ
댓글
권한이 없습니다. 로그인

신고

"님의 댓글"

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

삭제

"님의 댓글"

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

번호 분류 제목 글쓴이 날짜 조회 수
680 잡담
image
목포산이 19.05.03.17:13 511 13
679 잡담
image
title: 23/24 안드레 알메이다 (home)리빙스턴 19.04.29.09:22 235 3
678 잡담
image
Mestalla-Bat 19.04.29.07:24 344 10
677 잡담
image
title: #LIMGOHOMECHAPTERc 19.04.26.17:09 459 5
676 잡담
image
Mestalla-Bat 19.04.25.12:54 379 11
675 잡담
image
title: 23/24 안드레 알메이다 (home)리빙스턴 19.04.22.06:46 234 2
674 잡담
image
title: 23/24 안드레 알메이다 (home)리빙스턴 19.04.15.09:06 338 7
673 잡담
image
Mestalla-Bat 19.04.14.15:47 851 11
672 잡담
image
카스테얀 19.04.14.01:20 257 3
671 잡담
image
켈메 19.04.09.10:55 412 2
670 잡담
image
카탈리나앤파트너스 19.04.08.23:41 632 6
669 잡담
image
Pilgrim 19.04.07.03:26 536 1
668 잡담
image
Sam 19.04.06.18:37 304 3
667 잡담
image
켈메 19.04.05.17:10 324 2
666 잡담
image
title: 2018 WC 호드리구 모레노솔다도 19.04.03.12:41 221 2
665 잡담
image
휴이 19.04.02.23:27 575 15
잡담
image
title: #LIMGOHOMEVladimir 19.04.02.18:27 354 1
663 잡담
image
Joshh 19.04.02.16:57 535 5
662 잡담
image
title: 23/24 안드레 알메이다 (home)리빙스턴 19.04.01.09:12 315 2
661 게임
image
title: 2006 WC 다비드 알벨다Cinna 19.03.28.23:00 1474 2