마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

NullPointerException 에러

0 추천

인터넷에 올라와있는 FTP 클라이언트 예제 어플을 수정하여 안드로이드 FTP 클라이언트 어플리케이션을 제작중입니다.

 

어플에서 FTP 서버에 접속해서 다운로드를 하는 부분에서 에러가 나는데

코드상에는 에러가 없는데 로그기록에 NullPointerException 이라고 뜨는데 왜그런걸까요?

 

코드 내용을 하나하나 검색하면서 보니, 다운로드 후 다운 파일을 저장하는 경로가 외부저장소로 되어있어서 내부저장소로 바꾸면 되는 건 줄 알고 바꿨는데, 역시나 NullPointerException라고 뜹니다. 

package dc.mobdev.ftpremote.tasks;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import dc.mobdev.ftpremote.configs.FTPConnection;
import android.content.Context;
import android.os.Environment;

public class DownloadTask extends BaseTask{

	public DownloadTask(Context context){
		this.init(context);
	}
	
	private void init(Context context){
		this.context = context;
	}
	
	@Override
	protected Object doInBackground(Object... params) {
		String pathToFileDownload = (String)params[0];
		String fileNameToDownload = (String)params[1];
		String dirNameToSave = (String)params[2];	
		return download(pathToFileDownload, fileNameToDownload, dirNameToSave);
	}

	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		if((Boolean) result == true){
			System.out.println("Download Sucessful!");
		}
	}
	
	
	private boolean download(String pathToFileDownload, String fileNameToDownload, String dirNameToSave){
		boolean result = false;
		String pathToSave = null;
		File dir = null;
		File fileDownload;
		OutputStream os = null;
		FileOutputStream fos = null;

		try{
			String[] keys = FTPConnection.findKeys();
			String connectionStr = FTPConnection.readFromSharedPreferenceFile(keys);	
			this.openConnection(connectionStr);
			
			//pathToSave = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + dirNameToSave; 기존코드
			
			pathToSave = Environment.getDataDirectory().getAbsolutePath() + "/" + dirNameToSave; //수정1
			//pathToSave = getFilesDir().getAbsolutePath() + "/" + dirNameToSave; 수정2
			
			dir = new File(pathToSave);
			dir.mkdirs();
			
			fileDownload = new File(dir, fileNameToDownload);
			fos = new FileOutputStream(fileDownload);
			os = new BufferedOutputStream(fos);	
			result = this.ftpClient.retrieveFile(pathToFileDownload + "/" + fileNameToDownload, os);
			return result;
		}catch(FileNotFoundException fnfE){
			fnfE.printStackTrace();
			result = false;
			return result;
		}catch(IOException ioE) {
			ioE.printStackTrace();
			result = false;
			//result = true;
			return result;
		}finally{
			try {
				os.close();
				fos.close();
				this.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}	
	}
}

 

익명사용자 님이 2015년 5월 6일 질문

1개의 답변

0 추천
RuntimeException의 경우는 아무리 고수라도 소스코드만 올려서는 알기가 어렵습니다.

 로그 파일에 보면, DownloadTask.download 소스 87번째 줄에 에러가 난다고 나와있습니다.

 87번째 줄이 어떤 건지 모르나 보통은,  객체 생성을 하지 않고 변수에 값을 대입한다거나 함수를 호출하면 발생하는 문제입니다.
안_드루이드 (14,510 포인트) 님이 2015년 5월 7일 답변
...