2013-04-27 2 views
0

저는 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"); 
    } 
} 

이 사람이 나를 도울 수 있습니까? 당신이 첫 번째 매개 변수로 asetData 전화

+0

클래스 및 인터페이스는 대문자로 시작해야합니다. – TheEwook

+0

@ 올바르지 않습니다. 그들은 할 필요는 없지만 일반적인 Java 규칙입니다. – NilsH

답변

2

if (a==null) setData(a,artist,name,duration,fileSize);

a == null 경우 (null이다).

이제 setData에 당신이 할 : sin는 첫 번째 매개 변수입니다

sin.setArtist(artist);. 쓰기와 같습니다 :

null.setArtist(artist) 물론 물론 .. NPE를 던집니다.

추가 쪽 참고 : Java Naming Conventions을 따르는 것이 좋습니다. 이 글을 읽은 후에는 대문자로 시작하도록 클래스 이름을 변경하고 싶을 것입니다.

+1

또한, 나는 OP가'sin' 인스턴스 변수와 로컬'sin' 변수를 혼합하고있는 것으로 생각합니다. 그들에게 다른 이름을주는 것이 낫습니다. – NilsH

관련 문제