I am trying to make an app in Android Studio that would "memorize" my grades and subjects. Currently, I am working on a system that would allow me to make subjects.
Compiled it, got no errors, I open the app, the app crashes and I receive an error: "Attempt to invoke virtual method on a null object reference"
I think the string array has something to do with it.
This is my code:
//these are declared in "public class MainActivity extends AppCompatActivity"
LinearLayout linearLayout = findViewById(R.id.linearLayout);
String[] subjectNames;
int arrayCounter=-1;
String name;
//addSubject() gets called when a button is pressed
public void addSubject (View view) {
EditText editSubjectName = new EditText(this);
editSubjectName.setHint("Unesi ime predmeta");
editSubjectName.setSingleLine(true);
linearLayout.addView(editSubjectName);
TextView subjectName = new TextView(this);
subjectName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.addView(subjectName);
editSubjectName.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
name = editSubjectName.getText().toString();
arrayCounter++;
return true;
}
return false;
}
});
subjectNames[arrayCounter] = name;
}
Thanks in advance!