2014-07-19 2 views
0

내가 묻는 이유는 null 포인터 예외가 발생합니다. 이미지를 바이트 배열과 바이트 배열로 구문 분석하는 SRSI.java라는 클래스가 있습니다. 다른 클래스 계정의 setter 메서드에 저장 오전. 이제 다른 세 번째 클래스에서이 setter 값을 사용하여 getter 메서드를 사용하여 작업 클래스에서 말할 수 있습니다.하지만 NPE를 얻고 있습니다. // 아래 내가 처리하고 DB에 저장하고 있어요 SRSI 클래스의 코드는 같은 시간에 내가다른 클래스의 객체에 동일한 참조 변수 얻기 java

 String fromLocal ="D://1-123456.jpg"; 
     long accountId = account.getId(); 
     File file = new File(fromLocal); 
     System.out.println("Length--> " + file.length()); 
     InputStream inputStream = null; 
     byte[] bFile= null; 
     byte[] imageData = null; 
     try { 
      inputStream = new BufferedInputStream(new FileInputStream(file)); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); 
      bFile = new byte[8192]; 
      int count; 
      while((count = inputStream.read(bFile))> 0){ 
       baos.write(bFile, 0, count); 
      } 
      bFile = baos.toByteArray(); 
      if(bFile.length > 0){ 
       imageData = bFile; 
      } 
      baos.flush(); 
      inputStream.close(); 
     } catch (Exception ioe) { 
      //throw ioe; 
     } 
     //storing the above byte array which contains image into account class setter method here account is reference of account class. 
     account.setImageData(imageData); 


     //here what account class getter and setter methods looks like 
      private byte[] imageData; 

      public void setImageData(byte[] imageData){ 
      if(null!=imageData){ 
       this.imageData = imageData; 
      } 
     } 

      public byte[] getImageData(){ 
      return imageData; 
      } 
     Now what i need this same value in getter method in 3rd class i.e.Action.What i am doing is 
     Action action = new Action(); 
     action.getImageData(); 

내가 그것을 새로운 개체를 만드는 것이로 알고 세터 방법으로 저장하고 있으며 그 이유입니다 NPE를 반환합니다. 하지만 액션 클래스와 액션 클래스와 SRSI, 계정에 다른 패키지에 setter의 동일한 가치가 필요합니다.

미리 감사드립니다.

답변

0

예를 들어 두 객체를 서로 참조 할 수 있도록 객체를 할당 할 수 있습니다.

ob = obj;

0

코드는 약간의 의사 틱하지만 내가 제대로 이해한다면, 당신은 단지 문제는 각 인스턴스는 자신의 필드를 가지고 있다는 것입니다

action.setImageData(account.getImageData()); 
0

뭔가를해야합니다. 이 코딩 3 번째 클래스입니다 작성자 :

Action action = new Action(); 
action.getImageData(); 

이 당신에게 다른 인스턴스의 이미지 데이터에 대한 참조를 제공하지 않습니다.

대신 원래 액션 개체를 setAction(Action action) 메서드를 만들고 전달 된 Action 개체를 필드에 저장 한 다음 필요할 때 action.getInageDara()을 사용하여 3 클래스에 전달해야합니다.

관련 문제