안드로이드 스튜디오 on a null object reference 오류에 대해 질문드립니다.

조회수 1615회
package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.example.myapplication.Adapter.ChatAdapter;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class ChatActivity extends AppCompatActivity {
    EditText et;
    ListView listView;
    Button btsend;
    TextView tvname;
    ArrayList<MessageItem> messageItems=new ArrayList<>();
    ChatAdapter adapter;
    //Firebase Database 관리 객체참조변수
    FirebaseDatabase firebaseDatabase;
    //'chat'노드의 참조객체 참조변수
    DatabaseReference chatRef,reference;

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

        et=findViewById(R.id.et);
        btsend=findViewById(R.id.btnsend);
        listView=findViewById(R.id.listview);
        adapter=new ChatAdapter(messageItems,getLayoutInflater());
        listView.setAdapter(adapter);
        tvname = (TextView) findViewById(R.id.tvname);
        firebaseDatabase= FirebaseDatabase.getInstance();

        androidx.appcompat.widget.Toolbar mtoolbar = (androidx.appcompat.widget.Toolbar) findViewById(R.id.mtoolbar);
        setSupportActionBar(mtoolbar);
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mtoolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



        Intent intent = getIntent();
        String username = intent.getExtras().getString("userid");
        reference = firebaseDatabase.getReference("room").child(username);

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
               Gameinfo gminfo = dataSnapshot.getValue(Gameinfo.class);
                tvname.setText(gminfo.getId());
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

//        //Firebase DB관리
//        chatRef= firebaseDatabase.getReference("chatRoom").child("chat");
//
//        //firebase 실시간
//        chatRef.addChildEventListener(new ChildEventListener() {
//            //새로 추가된 것만 줌 ValueListener는 하나의 값만 바뀌어도 처음부터 다시 값을 줌
//            @Override
//            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
//
//                //새로 추가된 데이터(값 : MessageItem객체) 가져오기
//                Gameinfo gminfo = dataSnapshot.getValue(Gameinfo.class);
//                MessageItem messageItem= dataSnapshot.getValue(MessageItem.class);
//
//                //새로운 메세지를 리스뷰에 추가하기 위해 ArrayList에 추가
//                messageItems.add(messageItem);
//                tvname.setText(gminfo.getName());
//                //리스트뷰를 갱신
//                adapter.notifyDataSetChanged();
//                listView.setSelection(messageItems.size()-1); //리스트뷰의 마지막 위치로 스크롤 위치 이동
//            }
//
//            @Override
//            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
//
//            }
//
//            @Override
//            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
//
//            }
//
//            @Override
//            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
//
//            }
//
//            @Override
//            public void onCancelled(@NonNull DatabaseError databaseError) {
//
//            }
//        });
//
//        btsend.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String nickName= G.nickName;
//                String message= et.getText().toString();
//
//                //firebase에 저장할 값
//                MessageItem messageItem= new MessageItem(nickName,message);
//                //'char'노드에 MessageItem객체를 통해
//                chatRef.push().setValue(messageItem);
//
//                //EditText글 지우기
//                et.setText("");
//            }
//        });
    }

    private void setSupportActionBar(Toolbar toolbar) {
    }

    @Override
    public void onBackPressed(){
        Intent intentMatch =  new Intent(getApplicationContext(), match.class);
        startActivity(intentMatch);
    }
}

오류가 나는 코드입니다. 값이 다 저장이 되어있는데 도대체 뭐가 문제인지 모르겠습니다. 계속 실행 시키면 java.lang.String com.example.myapplication.Gameinfo.getId()' on a null object referenc 라는 오류만 뜹니다. 도와주세요

이것은 Gameinfo.java의 코드입니다.

package com.example.myapplication;

public class Gameinfo {
    private String name;
    private String name2;
    private String profile;
    private String profile2;
    private String id;

//    public Gameinfo(String name, String name2, String profile, String profile2, String id) {
//        this.name = name;
//        this.name2 = name2;
//        this.profile = profile;
//        this.profile2 = profile2;
//        this.id = id;
//    }

    public Gameinfo(){

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {  return name; }

    public void setName(String name) {
        this.name = name;
    }

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    public String getProfile2() {
        return profile2;
    }

    public void setProfile2(String profile2) {
        this.profile2 = profile2;
    }
}




  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)