저는 1 학년 컴퓨터 과학이며 1 학기에 우리의 과제는 자바로 간단한 음악 데이터베이스를 디자인하는 것입니다. 인터페이스 (모든 사용자 입력/출력 처리), 노래 (아티스트, 이름, 기간 및 파일 크기 저장) 및 데이터베이스 (4 곡 객체 저장, a, b, c, d)의 3 가지 클래스를 사용하고 있습니다. 나는 프로그램을 잘 컴파일하고 실행할 수 있지만 최근에 입력 한 정보를 반환하는 메시지 대신 마지막 필드 (fileSize)를 입력 할 때 NullPointerException을 받는다.이 것은 null 값을 할당하는 것과 관련이 있다는 것을 이해한다.NullPointerException이있는 간단한 자바 음악 데이터베이스
데이터베이스 클래스의 코드는 다음과 같습니다.
public class songDatabase
{
song sin = new song();
private song a,b,c,d;
public songDatabase()
{
a = null;
b = null;
c = null;
d = null;
}
public void addSong(String artist, String name, double duration, int fileSize)
{
if (a==null) setData(a,artist,name,duration,fileSize);
else if (b==null) setData(b,artist,name,duration,fileSize);
else if (c==null) setData(c,artist,name,duration,fileSize);
else if (d==null) setData(d,artist,name,duration,fileSize);
}
private void setData(song sin, String artist, String name, double duration, int fileSize)
{
sin.setArtist(artist);
sin.setName(name);
sin.setDuration(duration);
sin.setFileSize(fileSize);
}
public String visconfir()
{
if (a != null) return("You have imported: "+sin.getName()+"by"+sin.getArtist()+"which is"
+sin.getFileSize()+"kB and"+sin.getDuration()+"long(mm.ss)");
else return("Error - No file imported to database memory slot a");
}
}
이 사람이 나를 도울 수 있습니까? 당신이 첫 번째 매개 변수로 a
와 setData
전화
클래스 및 인터페이스는 대문자로 시작해야합니다. – TheEwook
@ 올바르지 않습니다. 그들은 할 필요는 없지만 일반적인 Java 규칙입니다. – NilsH