로그인 해주세요.


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

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

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

신고

"님의 댓글"

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

삭제

"님의 댓글"

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

번호 분류 제목 글쓴이 날짜 조회 수
2003 게임
image
하마 18.05.20.01:07 7309 2
2002 잡담
image
title: 2000 EURO 산티아고 카니사레스Banega 19.10.25.10:45 5442 8
2001 잡담
image
title: 2018 WC 호드리구 모레노명천 19.08.31.13:23 3869 3
2000 잡담
image
title: 2018 WC 에베르 바네가솔레르 18.08.04.20:44 2991 13
1999 잡담
image
title: 2022 WC 이강인KGLEE#10 18.08.08.21:44 2986 7
1998 잡담
image
게데스티니 19.11.09.12:48 2506 2
1997 잡담
image
El_Guaje 18.07.20.20:07 2479 5
1996 잡담
image
title: 2022 WC 이강인대가야황가야금관가야 22.10.19.21:46 2439 11
1995 게임
image
title: 2006 WC 헤드비게스 마두로월클솔레르 18.10.22.10:38 2363 4
1994 잡담
image
호아킹 18.03.02.10:22 2309 0
1993 잡담
image
title: 2002 WC 루벤 바라하DavidVilla 18.12.31.16:52 2304 3
1992 잡담
image
celeste 18.07.06.16:29 1769 8
1991 잡담
image
title: 2010 WC 박지성DavidSilva21 18.07.06.00:44 1746 2
1990 잡담
image
츅덕마켓 20.02.07.18:34 1682 13
1989 게임
image
title: #LIMGOHOME멘붕한라 20.02.21.21:06 1602 1
1988 잡담
image
title: 2006 WC 헤드비게스 마두로월클솔레르 18.01.11.05:47 1574 2
1987 잡담
image
title: #LIMGOHOME호랑무늬박쥐 20.05.25.08:55 1535 4
1986 게임
image
title: 2006 WC 다비드 알벨다Cinna 19.03.28.23:00 1477 2
1985 게임
image
Amunt 18.08.11.16:09 1468 1
1984 잡담
image
빙스턴 22.06.12.14:43 1437 14