I am trying to have a button within a popup send a variable to another class. However, when setting the on click listener I get this issue. I have initialized the button and tried to move the code to a different location. I have removed some of the code after the issue as it is not relevant
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.*;
import android.os.Bundle;
import android.view.*;
import android.util.*;
import android.widget.*;
import java.math.*;
public class MainActivity extends AppCompatActivity {
ImageButton imageButton, singleImageButton;
Integer width, height, minISO, maxISO, ISO, numberImages;
boolean focusable;
View popupView;
LayoutInflater inflater;
SeekBar ISOseekbar, exposureBar;
TextView ISOreadout, exposureReadout, textView;
String cameraId, numberImagesString;
Range<Integer> rangeISO;
Range<Long> exposureRange;
Long exposureMax, exposureMin, exposure;
BigDecimal exposureRoundedMax, exposureRoundedMin;
Button acceptNumberImages;
EditText numberImagesInput;
public void mainUI(){
setContentView(R.layout.activity_main);
imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.cameralayout, null);
width = LinearLayout.LayoutParams.WRAP_CONTENT;
height = LinearLayout.LayoutParams.WRAP_CONTENT;
focusable = true;
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
acceptNumberImages = findViewById(R.id.acceptNumberImages);
acceptNumberImages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberImagesInput = findViewById(R.id.numberImagesInput);
numberImagesString = numberImagesInput.getText().toString();
numberImages = Integer.parseInt(numberImagesString);
takePhoto.multipleImages(numberImages);
}
});
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
The full error that I get is:
Process: com.example.startrailscamera, PID: 18190
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.startrailscamera.MainActivity$1.onClick(MainActivity.java:52)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25921)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)