로그인 해주세요.


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

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

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

신고

"님의 댓글"

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

삭제

"님의 댓글"

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

번호 분류 제목 글쓴이 날짜 조회 수
2002 잡담
image
title: 2020 EURO 다니엘 바스goldenche 17.11.21.18:45 176 0
2001 잡담
image
QuiqueFlores 17.11.21.18:57 161 0
2000 잡담
image
title: 2012 EURO 후안 마타강미나 17.11.21.19:13 144 0
1999 잡담
image
title: 2018 WC 호드리구 모레노이제널안고가야 17.11.21.20:28 213 0
1998 잡담
image
El_Guaje 17.11.21.19:33 140 0
1997 잡담
image
그래도아직은호아킨이죠 17.11.21.19:33 181 0
1996 잡담
image
비센테 17.11.22.10:47 280 0
1995 잡담
image
휴이 17.11.21.19:35 129 0
1994 잡담
image
피니긔 17.11.21.19:43 143 0
1993 잡담
image
title: #LIMGOHOME아뭉 17.11.22.08:51 179 0
1992 잡담
image
Amunt 17.11.21.17:49 149 0
1991 잡담
image
title: 2006 WC 헤드비게스 마두로월클솔레르 17.11.22.10:57 292 0
1990 잡담
image
루디 17.11.21.16:42 176 0
1989 잡담
image
title: 2000 EURO 가이스카 멘디에타축덕 17.11.21.16:39 164 0
1988 잡담
image
etxeberria 17.11.21.16:59 211 0
1987 잡담
image
쎄피로쓰 17.11.21.17:00 237 0
1986 잡담
image
페데 17.11.21.17:01 145 0
1985 잡담
image
title: 05/06 파블로 아이마르아이마르 17.11.21.15:47 168 0
1984 잡담
image
title: 12/13 후안 마타 (첼시)마타 17.11.21.16:22 171 0
1983 잡담
image
Valencia 17.11.21.15:42 152 0