The error is: 'void android.widget.TableRow.setVisibility(int)' on a null object reference
Very new to android studio and java and have been working with it for a couple of weeks. Basically the problem is that I have multiple activities with multiple fragments each. One of these called Calculator has three fragments and in fragemntCalc2, I have a lot of table rows which I want to make invisible via a drop-down from a different activities fragment, specifically one called profile and from it's second fragment.
public class Fragment2 extends Fragment {
Context context;
Spinner CourseNameSpinner1;
Spinner QualificationSpinner1;
Spinner ExamboardSpinner1;
Spinner spinner_switch;
public static Fragment2 newInstance()
{
Fragment2 fragment = new Fragment2();
return fragment;
}
public Fragment2() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile2, container, false);
context = container.getContext();
CourseNameSpinner1 = (Spinner) rootView.findViewById(R.id.CourseNameSpinner1);
QualificationSpinner1 = (Spinner) rootView.findViewById(R.id.QualificationSpinner1);
ExamboardSpinner1 = (Spinner) rootView.findViewById(R.id.ExamboardSpinner1);
spinner_switch = (Spinner) rootView.findViewById(R.id.spinner_switch);
//Course Dropdown
Spinner spinner2 = (Spinner) rootView.findViewById(R.id.CourseNameSpinner1);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(context, R.array.Course_Array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
spinner2.setAdapter(adapter2); // Apply the adapter to the spinner
//Qualification Dropdown
Spinner spinner3 = (Spinner) rootView.findViewById(R.id.QualificationSpinner1);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(context, R.array.Qualification_Array, android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
spinner3.setAdapter(adapter3); // Apply the adapter to the spinner
//ExamBoard Dropdown
Spinner spinner4 = (Spinner) rootView.findViewById(R.id.ExamboardSpinner1);
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(context, R.array.Exam_Array, android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
spinner4.setAdapter(adapter4); // Apply the adapter to the spinner
Spinner spinner23 = (Spinner) rootView.findViewById(R.id.spinner_switch);
ArrayAdapter<CharSequence> adapter23 = ArrayAdapter.createFromResource(context, R.array.Switch_Array, android.R.layout.simple_spinner_item);
adapter23.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
spinner23.setAdapter(adapter23); // Apply the adapter to the spinner
//ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(context, R.array.Course_Array, android.R.layout.simple_spinner_item);
//Where context is, originally it was a this.
//Changed as the spinner isn't declared in main activity
//this is fragment java, therefore it needs context to declare the spinner
SharedPreferences sharedPref = context.getSharedPreferences("Units", context.MODE_PRIVATE);
int spinnerValue = sharedPref.getInt("UserCourseChoiceSpinner1", -1);
int spinnerValue2 = sharedPref.getInt("UserCourseChoiceSpinner2",-1);
int spinnerValue3 = sharedPref.getInt("UserCourseChoiceSpinner3",-1);
if(spinnerValue != -1)
CourseNameSpinner1.setSelection(spinnerValue);
QualificationSpinner1.setSelection(spinnerValue2);
ExamboardSpinner1.setSelection(spinnerValue3);
Button button = (Button) rootView.findViewById(R.id.button_savecourse);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int userChoice = CourseNameSpinner1.getSelectedItemPosition();
int userChoice2 = QualificationSpinner1.getSelectedItemPosition();
int userChoice3 = ExamboardSpinner1.getSelectedItemPosition();
SharedPreferences sharedPref = context.getSharedPreferences("Units", Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("UserCourseChoiceSpinner1",userChoice);
prefEditor.putInt("UserCourseChoiceSpinner2",userChoice2);
prefEditor.putInt("UserCourseChoiceSpinner3",userChoice3);
prefEditor.commit();
Toast.makeText(context, "Data was saved successfully", Toast.LENGTH_LONG).show();
}
});
Spinner spinner_switch = (Spinner)rootView.findViewById(R.id.spinner_switch);
spinner_switch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
TableRow tableRow;
if (position != 1)
{
tableRow = (TableRow) getActivity().findViewById(R.id.table13);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.table14);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.table15);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.table16);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.table17);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.table18);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade13);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade14);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade15);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade16);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade17);
tableRow.setVisibility(View.GONE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade18);
tableRow.setVisibility(View.GONE);
}
else
{
tableRow = (TableRow) getActivity().findViewById(R.id.table13);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.table14);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.table15);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.table16);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.table17);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.table18);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade13);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade14);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade15);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade16);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade17);
tableRow.setVisibility(View.VISIBLE);
tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade18);
tableRow.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
return rootView;
}
}
The null object reference is on the line
tableRow = (TableRow) getActivity().findViewById(R.id.table13);
tableRow.setVisibility(View.GONE);
so I'd assume the rest of it doesn't work. Again as info, the tables are on a completely different activity fragment so how would I make it so that the fragment can find the tablerows even though I am declaring context.
Change like this
tableRow = (TableRow) rootView.findViewById(R.id.table13);
tableRow.setVisibility(View.GONE);
and declare rootView
global,and you take seperate variable for all the TableRow
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.