# Errors with saveTable() on Android

**URL:** https://discourse.processing.org/t/errors-with-savetable-on-android/26546
**Category:** Processing for Android
**Created:** [December 28, 2020, 2:15am UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546 "2020-12-28T02:15:48Z")
**Posts on this page:** 14
**Page:** 2

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [December 30, 2020, 2:50pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/21 "2020-12-30T14:50:41Z")

</div>

I’m able to load the tables, but the game still crashes when it’s closed. I can’t really figure out the issue.

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [December 30, 2020, 3:09pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/22 "2020-12-30T15:09:46Z")

</div>

Maybe you could post your whole code?

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [December 30, 2020, 3:14pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/23 "2020-12-30T15:14:21Z")

</div>

It’s 800 lines, i don’t know if it’s too much. maybe I can post the save data code

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [December 30, 2020, 3:16pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/24 "2020-12-30T15:16:38Z")

</div>

Maybe you can post the part that shows how you exit the sketch. Because you do an automatic saveTable() at exit?

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [December 30, 2020, 3:18pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/25 "2020-12-30T15:18:05Z")

</div>

import android.os.Environment;  
import android.Manifest;  
import android.content.pm.PackageManager;  
import android.os.Build;  
import android.os.Build.VERSION\_CODES;  
import processing.core.PConstants;  
import android.app.Activity;  
import android.os.Environment;  
size(displayWidth, displayHeight);  
frameRate(60);  
orientation(PORTRAIT);  
save = true;  
for(int i = 0; i \< scores.length; i++) {  
scores[i] = 0;  
}  
table = null;  
loadData(tableFile);  
if(table != null){  
for(TableRow row : table.rows()){  
int id = row.getInt(“id”);  
scores[id] = row.getInt(“savedata”);  
}  
} else {  
makeTable();  
}  
p = new Player();  
gameTable();  
font = createFont(“ClearSans-Bold.ttf”, 64);  
icon = loadImage(“icon.png”);  
if(!loadSave) {  
p.addNewTile();  
p.addNewTile();  
} else {  
p.loadSaveFile();  
}  
}  
void makeTable() {  
table = new Table();  
table.addColumn(“id”);  
table.addColumn(“savedata”);  
for(int i = 0; i \< scores.length; i++){  
TableRow newRow = table.addRow();  
newRow.setInt(“id”, table.lastRowIndex());  
newRow.setInt(“savedata”, scores[i]);  
}  
}  
void loadData(String s) {  
try {  
String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+tableFolder+"/");  
table = loadTable(directory+s, “header”);  
println(directory+s+tableFile);  
}  
catch(Exception e) {  
println(“first table loading”);  
}  
}  
void loadData2(String s) {  
try {  
String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+tableFolder+"/");  
table2 = loadTable(directory+s, “header”);  
println(directory+s+tableFile2);  
}  
catch(Exception e) {  
println(“first table loading”);  
}  
}  
void saveData(String file\_s, String folder\_s) {  
try {  
String absolute\_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath());  
File file = new File(absolute\_path+"/"+folder\_s);  
println(file);  
if (!file.exists()) {  
boolean success = true;  
success = file.mkdirs();  
}  
saveTable(table, file+"/"+file\_s);  
println(“File saved successfully.”);  
}  
catch (Exception e) {  
println(“Error while saving file: " + e);  
}  
}  
void saveData2(String file\_s, String folder\_s) {  
try {  
String absolute\_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath());  
File file = new File(absolute\_path+”/"+folder\_s);  
println(file);  
if (!file.exists()) {  
boolean success = true;  
success = file.mkdirs();  
}  
saveTable(table, file+"/"+file\_s);  
println(“File saved successfully.”);  
}  
catch (Exception e) {  
println("Error while saving file: " + e);  
}  
}  
private static String[] PERMISSIONS\_STORAGE = { Manifest.permission.READ\_EXTERNAL\_STORAGE, Manifest.permission.WRITE\_EXTERNAL\_STORAGE };  
private void requestParticularPermission() {  
activity.requestPermissions(PERMISSIONS\_STORAGE, 2020);  
}  
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {  
switch (requestCode) {  
case 2020:  
if (grantResults[0] == PackageManager.PERMISSION\_GRANTED) {  
println(“permissions granted”);  
permissions\_granted = true;  
} else {  
println(“permissions not granted”);  
}  
break;  
default:  
activity.onRequestPermissionsResult(requestCode, permissions, grantResults);  
}  
}  
void draw() {  
highScore = max(scores);  
if(p.score \> highScore) {  
highScore = p.score;  
}  
if(scores[scores.length-1] != 0) {  
int savedScore = max(scores);  
for(int i = 0; i \< scores.length; i++) {  
scores[i] = 0;  
}  
scores[0] = savedScore;  
saveScores();  
}  
if(lose()) {  
if(save) {  
addNewScore(p.score);  
updateTable();  
saveData(tableFile, tableFolder);  
save = false;  
}  
}  
}  
void addNewScore(int score){  
for(int i = 0; i \< scores.length; i++) {  
if(score \>= scores[i]) {  
for(int j = scores.length-1; j \>= max(i,1); j–) {  
scores[j] = scores[j-1];  
}  
scores[i] = score;  
break;  
}  
}  
}  
void updateTable() {  
for(TableRow row : table.rows()) {  
int id = row.getInt(“id”);  
table.setInt(id, “savedata”, scores[id]);  
}  
}  
void saveScores(){  
saveData(tableFile, tableFolder);  
}  
void saveGame() {  
for(int i = 0; i \< 4; i++) {  
for(int j = 0; j \< 4; j++) {  
game[i][j] = p.getValue(i, j);  
}  
}  
}  
void gameTable() {  
saveGame();  
table2 = null;  
loadData2(tableFile2);  
if(table2 != null) {  
for(int i = 0; i \< 4; i++) {  
TableRow row = table2.getRow(i);  
game[0][i] = row.getInt(“col0”);  
game[1][i] = row.getInt(“col1”);  
game[2][i] = row.getInt(“col2”);  
game[3][i] = row.getInt(“col3”);  
}  
loadSave = checkTable();  
} else {  
makeTable2();  
}  
}  
void makeTable2() {  
table2 = new Table();  
table2.addColumn(“col0”);  
table2.addColumn(“col1”);  
table2.addColumn(“col2”);  
table2.addColumn(“col3”);  
for(int i = 0; i \< game.length; i++) {  
TableRow newRow = table2.addRow();  
newRow.setInt(“col0”, game[0][i]);  
newRow.setInt(“col1”, game[1][i]);  
newRow.setInt(“col2”, game[2][i]);  
newRow.setInt(“col3”, game[3][i]);  
}  
loadSave = false;  
}  
void updateSave() {  
saveGame();  
for(int i = 0; i \< 4; i++) {  
TableRow row = table2.getRow(i);  
row.setInt(“col0”, game[0][i]);  
row.setInt(“col1”, game[1][i]);  
row.setInt(“col2”, game[2][i]);  
row.setInt(“col3”, game[3][i]);  
}  
}  
boolean checkTable() {  
for(int i = 0; i \< 4; i++) {  
if(table2.getInt(i, “col0”) \> 0) {  
return true;  
}  
if(table2.getInt(i, “col1”) \> 0) {  
return true;  
}  
if(table2.getInt(i, “col2”) \> 0) {  
return true;  
}  
if(table2.getInt(i, “col3”) \> 0) {  
return true;  
}  
}  
return false;  
}  
void clearTable() {  
for(int i = 0; i \< 4; i++) {  
table2.setInt(i, “col0”, 0);  
table2.setInt(i, “col1”, 0);  
table2.setInt(i, “col2”, 0);  
table2.setInt(i, “col3”, 0);  
}  
saveData2(tableFile2, tableFolder);  
}  
void exit() {  
gameTable();  
updateSave();  
saveData2(tableFile2, tableFolder);  
super.exit();  
}

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [December 30, 2020, 3:19pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/26 "2020-12-30T15:19:04Z")

</div>

That was all of the savedata code. This is the code that runs when the program exits:  
void exit() {  
gameTable();  
updateSave();  
saveData2(tableFile2, tableFolder);  
super.exit();  
}

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [December 31, 2020, 6:54pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/27 "2020-12-31T18:54:54Z")

</div>

@noel === i tested the code you have put for saving table; it runs fine (MM, Nougat)

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 31, 2020, 6:59pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/28 "2020-12-31T18:59:26Z")

</div>

Hello @SNICKRS,

[Egads](https://www.thefreedictionary.com/egads)!

Please format your code:  
[https://discourse.processing.org/faq](https://discourse.processing.org/faq) \< See section on formatting.

You can also use the “Hide Details”… click on the ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1514f69409e9fc574160d45dd072ee43ece4a5fa.png)for that when editing.

> **Example**
>
> You can put formatted code here…
> 
> For example:
> 
> ```auto
> void setup() 
> {
> size(640, 360);
> }
> 
> void draw() 
> {
> background(0);
> }
> 
> ```

`:)`

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [January 3, 2021, 3:42pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/29 "2021-01-03T15:42:29Z")

</div>

@SNICKRS === diifficult to help you because the code you put is wrong ( eg . what is player…what is game ), quite all var are undefined, some of them we can guess but that is not good…

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [January 3, 2021, 6:03pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/30 "2021-01-03T18:03:18Z")

</div>

Oh, well then maybe I should send the entire code for the game. Maybe that would make it easier. I’m new to this, so I don’t really know much about the process.

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [January 3, 2021, 7:38pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/31 "2021-01-03T19:38:15Z")

</div>

Hello again, I tested my game with the updated code that @noel posted, and I’m able to save the game’s data! Now, the problem is that it doesn’t load it, but it’s in the sdcard!

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [January 3, 2021, 7:46pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/32 "2021-01-03T19:46:57Z")

</div>

I am sure that you will encounter a solution because if you could save, then there is no reason to not be able to read. The problem is in your code above, which btw you did not edit yet, putting it in a real code block by selecting solely the whole code and pressing the \<\> button.

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [January 3, 2021, 7:59pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/33 "2021-01-03T19:59:33Z")

</div>

Okay, the problem is officially solved! I was doing something wrong with the loadTable function, but it is working now! Thank you to @noel, @akenaton, and @paulgoux! For anyone with the same problem, please use @noel’s code because it works!

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [January 4, 2021, 2:06pm UTC](https://discourse.processing.org/t/errors-with-savetable-on-android/26546/35 "2021-01-04T14:06:45Z")

</div>

In order to save to internal storage, you can use getContext().getFilesDir().getAbsolutePath() too.

[Previous page](https://discourse.processing.org/t/errors-with-savetable-on-android/26546.md?page=1)
