Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit d2affad

Browse files
author
Evgenii Kanivets
committed
#33[30m]. Fix NPE with removed accounts.
1 parent 4a1a86b commit d2affad

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/AddRecordActivity.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ protected void initViews() {
102102
//Add texts to dialog if it's edit dialog
103103
if (mode == Mode.MODE_EDIT) {
104104
etTitle.setText(record.getTitle());
105-
etCategory.setText(record.getCategory().getName());
105+
if (record.getCategory() != null) etCategory.setText(record.getCategory().getName());
106106
etPrice.setText(Integer.toString(record.getPrice()));
107107

108-
for (int i = 0; i < accountList.size(); i++) {
109-
Account account = accountList.get(i);
110-
if (account.getId() == record.getAccount().getId()) {
111-
spinnerAccount.setSelection(i);
108+
if (record.getAccount() != null) {
109+
for (int i = 0; i < accountList.size(); i++) {
110+
Account account = accountList.get(i);
111+
if (account.getId() == record.getAccount().getId()) {
112+
spinnerAccount.setSelection(i);
113+
}
112114
}
113115
}
114116
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/AccountController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public AccountController(IRepo<Account> accountRepo) {
2323
}
2424

2525
public boolean recordAdded(@Nullable Record record) {
26-
if (record == null) return false;
26+
if (record == null || record.getAccount() == null) return false;
2727

2828
Account account = repo.read(record.getAccount().getId());
2929
if (account == null) return false;
@@ -47,7 +47,7 @@ public boolean recordAdded(@Nullable Record record) {
4747
}
4848

4949
public boolean recordDeleted(@Nullable Record record) {
50-
if (record == null) return false;
50+
if (record == null || record.getAccount() == null) return false;
5151

5252
Account account = repo.read(record.getAccount().getId());
5353
if (account == null) return false;

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/RecordController.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ public List<Record> readWithCondition(String condition, String[] args) {
8989
// Data read from DB through Repo layer doesn't contain right nested objects, so construct them
9090
List<Record> completedRecordList = new ArrayList<>();
9191
for (Record record : recordList) {
92-
Category category = categoryController.read(record.getCategory().getId());
93-
Account account = accountController.read(record.getAccount().getId());
92+
Category category = null;
93+
if (record.getCategory() != null)
94+
category = categoryController.read(record.getCategory().getId());
95+
96+
Account account = null;
97+
if (record.getAccount() != null)
98+
account = accountController.read(record.getAccount().getId());
9499

95100
completedRecordList.add(new Record(record.getId(), record.getTime(), record.getType(),
96101
record.getTitle(), category, record.getPrice(), account, record.getCurrency()));
@@ -132,7 +137,11 @@ public List<String> getRecordsForExport(long fromDate, long toDate) {
132137
sb.append(record.getId()).append(DELIMITER);
133138
sb.append(record.getTime()).append(DELIMITER);
134139
sb.append(record.getTitle()).append(DELIMITER);
135-
Category category = categoryController.read(record.getCategory().getId());
140+
141+
Category category = null;
142+
if (record.getCategory() != null)
143+
category = categoryController.read(record.getCategory().getId());
144+
136145
sb.append(category == null ? "NONE" : category.getName()).append(DELIMITER);
137146
sb.append(record.getType() == 0 ? record.getPrice() : -record.getPrice());
138147

@@ -143,6 +152,8 @@ public List<String> getRecordsForExport(long fromDate, long toDate) {
143152
}
144153

145154
private Record validateRecord(Record record) {
155+
if (record.getCategory() == null) return record;
156+
146157
Category category = categoryController.readOrCreate(record.getCategory().getName());
147158

148159
return new Record(record.getId(), record.getTime(), record.getType(), record.getTitle(),

app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/Record.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.os.Parcel;
44
import android.os.Parcelable;
5+
import android.support.annotation.Nullable;
56

67
import com.blogspot.e_kanivets.moneytracker.DbHelper;
78
import com.blogspot.e_kanivets.moneytracker.entity.base.BaseEntity;
@@ -91,6 +92,7 @@ public String getTitle() {
9192
return title;
9293
}
9394

95+
@Nullable
9496
public Category getCategory() {
9597
return category;
9698
}
@@ -107,6 +109,7 @@ public boolean isIncome() {
107109
return type == 0;
108110
}
109111

112+
@Nullable
110113
public Account getAccount() {
111114
return account;
112115
}

0 commit comments

Comments
 (0)