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

NullPointerException이 발생합니다.

0 추천

안드로이드를 시작한지 얼마안된 초보자입니다.

간단한 문제인거같은데 해결을 못하고있습니다.

package com.abcd.example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    Times times;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        System.out.println(times.formatDate);
    }
}
package com.abcd.example;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Times {
    private long now = System.currentTimeMillis();
    private Date date = new Date(now);
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    public String formatDate = simpleDateFormat.format(date);
}

MainActivity.java 클래스안에 전부 코드를 입력하면 실행이되는데

클래스를 분리하여 객체를 이용하면 NullPointerException이 발생합니다.

 

이진규 (340 포인트) 님이 2018년 2월 12일 질문
02-12 11:19:07.665 11476-11476/com.abcd.example E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.abcd.example, PID: 11476
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abcd.example/com.abcd.example.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.abcd.example.Times.formatDate' on a null object reference
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3150)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
                                                                            at android.app.ActivityThread.access$1000(ActivityThread.java:218)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:145)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                         Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.abcd.example.Times.formatDate' on a null object reference
                                                                            at com.abcd.example.MainActivity.onCreate(MainActivity.java:14)
                                                                            at android.app.Activity.performCreate(Activity.java:6609)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
                                                                            at android.app.ActivityThread.access$1000(ActivityThread.java:218)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:145)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
02-12 11:24:08.058 11476-11476/com.abcd.example I/Process: Sending signal. PID: 11476 SIG: 9
글자수 제한때문에 댓글에 남겼어요
public class Times -> public static class Times
Modifier 'static' not allowed here

라고 떠요
onCreate 에 times = new Times();
를 선언해주세요, 형만 존재하고 알맹이는 없는 상태입니다

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...