Problem with SQLite in fragment after start app [duplicate]
This question already has an answer here:
Android: NullPointerException Unable to load database into listview within a fragment
1 answer
Why is my context in my Fragment null?
3 answers
Unable to get data from sqlite database
1 answer
I have a problem with SQLite database. App won't start because of:
2018-11-22 01:27:05.679 13880-13880/com.cewe.martin.cewetracking E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cewe.martin.cewetracking, PID: 13880
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cewe.martin.cewetracking/com.cewe.martin.cewetracking.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
I am starting my app with MainActivity with fragment inside. I found that I must initialize database before fragment is loaded, so I made this
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//HERE WHAT I ADDED
DataModel dm = new DataModel(this);
ArrayList<HashMap<String, String>> dfmka = dm.dejZaznamy();
//HERE
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
...
My fragment:
public class ScanFragment extends Fragment {
private EditText rucneKod;
private int odpovedKod;
private EditText rucneSberna;
private Switch jeDFM;
//HERE DataModel dm = new DataModel(getActivity());
private ListView lw;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_scan, container, false);
Button skenovaciTlacitko = view.findViewById(R.id.button2);
skenovaciTlacitko.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
zapnoutSkener();
}
});
rucneKod = view.findViewById(R.id.numberInput);
rucneSberna = view.findViewById(R.id.sbernaSkenInput);
jeDFM = (Switch) view.findViewById(R.id.switch1);
jeDFM.setChecked(false);
rucneSberna.setVisibility(View.GONE);
lw = view.findViewById(R.id.list);
//AND HERE IS PART ABOUT SQL
ArrayList<HashMap<String, String>> dfmka = dm.dejZaznamy();
if (dfmka.size() != 0) {
ListAdapter adapter = new SimpleAdapter(getContext(), dfmka, R.layout.polozka, new String{"_id", "sn", "sberna"}, new int{R.id.dfmID, R.id.dfmSerial, R.id.dfmSberna});
lw.setAdapter(adapter);
}
Thanks for helping
EDIT: Thanks Mike M.. I also found this thread, but I do not understand kotlin well. I also found, that the style there is old and now I must use this. This is what I know now.


marked as duplicate by Mike M.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Android: NullPointerException Unable to load database into listview within a fragment
1 answer
Why is my context in my Fragment null?
3 answers
Unable to get data from sqlite database
1 answer
I have a problem with SQLite database. App won't start because of:
2018-11-22 01:27:05.679 13880-13880/com.cewe.martin.cewetracking E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cewe.martin.cewetracking, PID: 13880
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cewe.martin.cewetracking/com.cewe.martin.cewetracking.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
I am starting my app with MainActivity with fragment inside. I found that I must initialize database before fragment is loaded, so I made this
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//HERE WHAT I ADDED
DataModel dm = new DataModel(this);
ArrayList<HashMap<String, String>> dfmka = dm.dejZaznamy();
//HERE
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
...
My fragment:
public class ScanFragment extends Fragment {
private EditText rucneKod;
private int odpovedKod;
private EditText rucneSberna;
private Switch jeDFM;
//HERE DataModel dm = new DataModel(getActivity());
private ListView lw;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_scan, container, false);
Button skenovaciTlacitko = view.findViewById(R.id.button2);
skenovaciTlacitko.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
zapnoutSkener();
}
});
rucneKod = view.findViewById(R.id.numberInput);
rucneSberna = view.findViewById(R.id.sbernaSkenInput);
jeDFM = (Switch) view.findViewById(R.id.switch1);
jeDFM.setChecked(false);
rucneSberna.setVisibility(View.GONE);
lw = view.findViewById(R.id.list);
//AND HERE IS PART ABOUT SQL
ArrayList<HashMap<String, String>> dfmka = dm.dejZaznamy();
if (dfmka.size() != 0) {
ListAdapter adapter = new SimpleAdapter(getContext(), dfmka, R.layout.polozka, new String{"_id", "sn", "sberna"}, new int{R.id.dfmID, R.id.dfmSerial, R.id.dfmSberna});
lw.setAdapter(adapter);
}
Thanks for helping
EDIT: Thanks Mike M.. I also found this thread, but I do not understand kotlin well. I also found, that the style there is old and now I must use this. This is what I know now.


marked as duplicate by Mike M.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
In theFragment
,getActivity()
will return null in a field initializer. Move theDataModel
instantiation into an appropriate lifecycle method; e.g.,onActivityCreated()
.
– Mike M.
Nov 22 '18 at 0:50
Whoops. I just realized the duplicate I linked is for Kotlin. Hang on, I'll find a Java duplicate.
– Mike M.
Nov 22 '18 at 0:51
I Edited an ask, that i found something. But that java would be better :-). I do not know kotlin. Can you please remove, that is duplicate? Thanks
– M. Cahyna
Nov 22 '18 at 1:05
Oh, jeez, yeah, I meantonAttach()
, notonActivityCreated()
. I really fumbled this one, didn't I? Sorry about that. Glad you figured it out. I'll update the duplicate link here in a minute. Cheers!
– Mike M.
Nov 22 '18 at 1:08
There we go. Added a Java duplicate, though you don't really need to know Kotlin to follow the answer on the other, so I'll leave that link, too. The lifecycle methods are still the same in either language.
– Mike M.
Nov 22 '18 at 1:20
add a comment |
This question already has an answer here:
Android: NullPointerException Unable to load database into listview within a fragment
1 answer
Why is my context in my Fragment null?
3 answers
Unable to get data from sqlite database
1 answer
I have a problem with SQLite database. App won't start because of:
2018-11-22 01:27:05.679 13880-13880/com.cewe.martin.cewetracking E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cewe.martin.cewetracking, PID: 13880
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cewe.martin.cewetracking/com.cewe.martin.cewetracking.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
I am starting my app with MainActivity with fragment inside. I found that I must initialize database before fragment is loaded, so I made this
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//HERE WHAT I ADDED
DataModel dm = new DataModel(this);
ArrayList<HashMap<String, String>> dfmka = dm.dejZaznamy();
//HERE
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
...
My fragment:
public class ScanFragment extends Fragment {
private EditText rucneKod;
private int odpovedKod;
private EditText rucneSberna;
private Switch jeDFM;
//HERE DataModel dm = new DataModel(getActivity());
private ListView lw;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_scan, container, false);
Button skenovaciTlacitko = view.findViewById(R.id.button2);
skenovaciTlacitko.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
zapnoutSkener();
}
});
rucneKod = view.findViewById(R.id.numberInput);
rucneSberna = view.findViewById(R.id.sbernaSkenInput);
jeDFM = (Switch) view.findViewById(R.id.switch1);
jeDFM.setChecked(false);
rucneSberna.setVisibility(View.GONE);
lw = view.findViewById(R.id.list);
//AND HERE IS PART ABOUT SQL
ArrayList<HashMap<String, String>> dfmka = dm.dejZaznamy();
if (dfmka.size() != 0) {
ListAdapter adapter = new SimpleAdapter(getContext(), dfmka, R.layout.polozka, new String{"_id", "sn", "sberna"}, new int{R.id.dfmID, R.id.dfmSerial, R.id.dfmSberna});
lw.setAdapter(adapter);
}
Thanks for helping
EDIT: Thanks Mike M.. I also found this thread, but I do not understand kotlin well. I also found, that the style there is old and now I must use this. This is what I know now.


This question already has an answer here:
Android: NullPointerException Unable to load database into listview within a fragment
1 answer
Why is my context in my Fragment null?
3 answers
Unable to get data from sqlite database
1 answer
I have a problem with SQLite database. App won't start because of:
2018-11-22 01:27:05.679 13880-13880/com.cewe.martin.cewetracking E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cewe.martin.cewetracking, PID: 13880
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cewe.martin.cewetracking/com.cewe.martin.cewetracking.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
I am starting my app with MainActivity with fragment inside. I found that I must initialize database before fragment is loaded, so I made this
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//HERE WHAT I ADDED
DataModel dm = new DataModel(this);
ArrayList<HashMap<String, String>> dfmka = dm.dejZaznamy();
//HERE
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
...
My fragment:
public class ScanFragment extends Fragment {
private EditText rucneKod;
private int odpovedKod;
private EditText rucneSberna;
private Switch jeDFM;
//HERE DataModel dm = new DataModel(getActivity());
private ListView lw;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_scan, container, false);
Button skenovaciTlacitko = view.findViewById(R.id.button2);
skenovaciTlacitko.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
zapnoutSkener();
}
});
rucneKod = view.findViewById(R.id.numberInput);
rucneSberna = view.findViewById(R.id.sbernaSkenInput);
jeDFM = (Switch) view.findViewById(R.id.switch1);
jeDFM.setChecked(false);
rucneSberna.setVisibility(View.GONE);
lw = view.findViewById(R.id.list);
//AND HERE IS PART ABOUT SQL
ArrayList<HashMap<String, String>> dfmka = dm.dejZaznamy();
if (dfmka.size() != 0) {
ListAdapter adapter = new SimpleAdapter(getContext(), dfmka, R.layout.polozka, new String{"_id", "sn", "sberna"}, new int{R.id.dfmID, R.id.dfmSerial, R.id.dfmSberna});
lw.setAdapter(adapter);
}
Thanks for helping
EDIT: Thanks Mike M.. I also found this thread, but I do not understand kotlin well. I also found, that the style there is old and now I must use this. This is what I know now.
This question already has an answer here:
Android: NullPointerException Unable to load database into listview within a fragment
1 answer
Why is my context in my Fragment null?
3 answers
Unable to get data from sqlite database
1 answer




edited Nov 22 '18 at 1:02
M. Cahyna
asked Nov 22 '18 at 0:42
M. CahynaM. Cahyna
86
86
marked as duplicate by Mike M.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mike M.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
In theFragment
,getActivity()
will return null in a field initializer. Move theDataModel
instantiation into an appropriate lifecycle method; e.g.,onActivityCreated()
.
– Mike M.
Nov 22 '18 at 0:50
Whoops. I just realized the duplicate I linked is for Kotlin. Hang on, I'll find a Java duplicate.
– Mike M.
Nov 22 '18 at 0:51
I Edited an ask, that i found something. But that java would be better :-). I do not know kotlin. Can you please remove, that is duplicate? Thanks
– M. Cahyna
Nov 22 '18 at 1:05
Oh, jeez, yeah, I meantonAttach()
, notonActivityCreated()
. I really fumbled this one, didn't I? Sorry about that. Glad you figured it out. I'll update the duplicate link here in a minute. Cheers!
– Mike M.
Nov 22 '18 at 1:08
There we go. Added a Java duplicate, though you don't really need to know Kotlin to follow the answer on the other, so I'll leave that link, too. The lifecycle methods are still the same in either language.
– Mike M.
Nov 22 '18 at 1:20
add a comment |
In theFragment
,getActivity()
will return null in a field initializer. Move theDataModel
instantiation into an appropriate lifecycle method; e.g.,onActivityCreated()
.
– Mike M.
Nov 22 '18 at 0:50
Whoops. I just realized the duplicate I linked is for Kotlin. Hang on, I'll find a Java duplicate.
– Mike M.
Nov 22 '18 at 0:51
I Edited an ask, that i found something. But that java would be better :-). I do not know kotlin. Can you please remove, that is duplicate? Thanks
– M. Cahyna
Nov 22 '18 at 1:05
Oh, jeez, yeah, I meantonAttach()
, notonActivityCreated()
. I really fumbled this one, didn't I? Sorry about that. Glad you figured it out. I'll update the duplicate link here in a minute. Cheers!
– Mike M.
Nov 22 '18 at 1:08
There we go. Added a Java duplicate, though you don't really need to know Kotlin to follow the answer on the other, so I'll leave that link, too. The lifecycle methods are still the same in either language.
– Mike M.
Nov 22 '18 at 1:20
In the
Fragment
, getActivity()
will return null in a field initializer. Move the DataModel
instantiation into an appropriate lifecycle method; e.g., onActivityCreated()
.– Mike M.
Nov 22 '18 at 0:50
In the
Fragment
, getActivity()
will return null in a field initializer. Move the DataModel
instantiation into an appropriate lifecycle method; e.g., onActivityCreated()
.– Mike M.
Nov 22 '18 at 0:50
Whoops. I just realized the duplicate I linked is for Kotlin. Hang on, I'll find a Java duplicate.
– Mike M.
Nov 22 '18 at 0:51
Whoops. I just realized the duplicate I linked is for Kotlin. Hang on, I'll find a Java duplicate.
– Mike M.
Nov 22 '18 at 0:51
I Edited an ask, that i found something. But that java would be better :-). I do not know kotlin. Can you please remove, that is duplicate? Thanks
– M. Cahyna
Nov 22 '18 at 1:05
I Edited an ask, that i found something. But that java would be better :-). I do not know kotlin. Can you please remove, that is duplicate? Thanks
– M. Cahyna
Nov 22 '18 at 1:05
Oh, jeez, yeah, I meant
onAttach()
, not onActivityCreated()
. I really fumbled this one, didn't I? Sorry about that. Glad you figured it out. I'll update the duplicate link here in a minute. Cheers!– Mike M.
Nov 22 '18 at 1:08
Oh, jeez, yeah, I meant
onAttach()
, not onActivityCreated()
. I really fumbled this one, didn't I? Sorry about that. Glad you figured it out. I'll update the duplicate link here in a minute. Cheers!– Mike M.
Nov 22 '18 at 1:08
There we go. Added a Java duplicate, though you don't really need to know Kotlin to follow the answer on the other, so I'll leave that link, too. The lifecycle methods are still the same in either language.
– Mike M.
Nov 22 '18 at 1:20
There we go. Added a Java duplicate, though you don't really need to know Kotlin to follow the answer on the other, so I'll leave that link, too. The lifecycle methods are still the same in either language.
– Mike M.
Nov 22 '18 at 1:20
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the
Fragment
,getActivity()
will return null in a field initializer. Move theDataModel
instantiation into an appropriate lifecycle method; e.g.,onActivityCreated()
.– Mike M.
Nov 22 '18 at 0:50
Whoops. I just realized the duplicate I linked is for Kotlin. Hang on, I'll find a Java duplicate.
– Mike M.
Nov 22 '18 at 0:51
I Edited an ask, that i found something. But that java would be better :-). I do not know kotlin. Can you please remove, that is duplicate? Thanks
– M. Cahyna
Nov 22 '18 at 1:05
Oh, jeez, yeah, I meant
onAttach()
, notonActivityCreated()
. I really fumbled this one, didn't I? Sorry about that. Glad you figured it out. I'll update the duplicate link here in a minute. Cheers!– Mike M.
Nov 22 '18 at 1:08
There we go. Added a Java duplicate, though you don't really need to know Kotlin to follow the answer on the other, so I'll leave that link, too. The lifecycle methods are still the same in either language.
– Mike M.
Nov 22 '18 at 1:20