로그인 해주세요.


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

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

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

신고

"님의 댓글"

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

삭제

"님의 댓글"

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

번호 분류 제목 글쓴이 날짜 조회 수
1585 잡담
image
꼬레아노 19.02.12.19:35 542 1
1584 잡담
image
덴뿌라우동 19.02.18.14:37 912 1
1583 잡담
image
title: #LIMGOHOME라치치 19.02.26.01:00 188 1
1582 잡담
image
애기박쥐 19.03.06.00:18 496 1
1581 잡담
image
title: 2018 WC 호드리구 모레노솔다도 19.03.07.15:31 747 1
1580 잡담
image
channy36 19.03.10.16:29 323 1
1579 잡담
image
title: 23/24 안드레 알메이다 (home)리빙스턴 19.03.11.07:48 283 1
1578 잡담
image
아카풀코 19.03.13.20:11 741 1
1577 잡담
image
title: 23/24 안드레 알메이다 (home)리빙스턴 19.03.18.11:58 284 1
잡담
image
title: #LIMGOHOMEVladimir 19.04.02.18:27 354 1
1575 잡담
image
Pilgrim 19.04.07.03:26 536 1
1574 잡담
image
트레네 19.05.04.21:05 264 1
1573 잡담
image
리빙스턴봇 19.05.06.17:41 289 1
1572 잡담
image
테트로도톡신 19.05.13.20:26 347 1
1571 잡담
image
지미닝스 19.06.12.16:11 333 1
1570 잡담
image
title: 23/24 안드레 알메이다 (home)리빙스턴 19.06.17.12:04 318 1
1569 잡담
image
AINO 19.07.05.14:32 631 1
1568 잡담
image
title: 2002 WC 루벤 바라하DavidVilla 19.07.12.13:16 419 1
1567 잡담
image
그래도아직은호아킨이죠 19.07.15.00:04 396 1
1566 게임
image
ㅇㅅㅋ보다내가못한게뭐야 19.07.16.10:07 752 1