Adding values into DataPoint array causing the application crash while running - java

I am using GraphView library code which contains DataPoint[] Array.
My code also contains a database named olddb which I use to insert the array length in the code below.
The first 2 for loops are for adding the values in the database into an array and the third for loop is to get the values out from the arrays arrayWeight and arrayId and add them to DataPoint[] array.
Though something seems to go wrong, after I run the program it just crashes, can anyone figure out what is causing the application to crash?
private DataPoint[] getDataPoint() {
if (olddb.check()) {
List<oldDetails> details = olddb.getDetails();
double[] arrayWeight = new double[olddb.getDetailsCount()];
int[] arrayId = new int[olddb.getDetailsCount()];
for (oldDetails cn : details) { //Adding weights and id of all time to an array.
double num = cn.getWeight();
int id = cn.getId();
for (int i = arrayWeight.length; i > 0; i--) {
arrayWeight[i] = num;
arrayId[i] = id;
}
}
DataPoint[] dp = new DataPoint[olddb.getDetailsCount()];
for (int i = 0; i < arrayId.length; i++) {
for (int j = 0; j < arrayWeight.length; j++)
dp[i] = new DataPoint(i, arrayWeight[j]);
}
return dp;
}
else {
DataPoint[] dp = new DataPoint[]{
new DataPoint(0, 0)
};
return dp;
}
}
Here is an example for working code of DataPoint array:
private DataPoint[] getDataPoint(){
DataPoint[] dp = new DataPoint[]{
new DataPoint(0,1),
new DataPoint(2,5),
new DataPoint(5,5),
new DataPoint(7,4)
};
return dp;
}
EDIT:
here is the error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.none.myapplication, PID: 3881
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.none.myapplication/com.none.myapplication.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM oldDetails
Database

You can fix it by replacing all 3 calls to olddb.getDetailsCount()
with
int count = olddb.getDetailsCount();
then:
double[] arrayWeight = new double[count];
int[] arrayId = new int[count];
DataPoint[] dp = new DataPoint[count];
However you need to understand the issue:
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM oldDetails is the error, which happens inside of DatabaseHandlerOld.getDetailsCount line 141
Android SQLite DB When to Close

Well after some researches and some of Blundell help, I figured out the problem was indeed the close(); method in my database.
Though the solution was to change the getDetailsCount() function to this:
public long QueryNumEntries()
{
SQLiteDatabase db = this.getReadableDatabase();
return DatabaseUtils.queryNumEntries(db, TABLE_OLDDETAILS);
}
and in my main activity file:
private DataPoint[] getDataPoint() {
if (olddb.check()) {
List<oldDetails> details = olddb.getDetails();
long c = olddb.QueryNumEntries();
int count = (int) c;
Log.d("Count", "equals: " + count);
double[] arrayWeight = new double[count];
int[] arrayId = new int[count];
for (oldDetails cn : details) { //Adding weights and id of all time to an array.
double num = cn.getWeight();
int id = cn.getId();
Log.d("num", "equals: " + num);
Log.d("id", "equals: " + id);
for (int i = count; i < 0; i--) {
arrayWeight[i] = num;
arrayId[i] = id;
}
}
DataPoint[] dp = new DataPoint[count];
for (int i = 0; i < arrayId.length; i++) {
for (int j = 0; j < arrayWeight.length; j++) {
dp[i] = new DataPoint(arrayId[i], arrayWeight[j]);
Log.d("ArrayWeight", "equals: " + arrayWeight[j]);
Log.d("ArrayId", "equals: " + arrayId[i]);
}
}
return dp;
}
DataPoint[] dp = new DataPoint[]{
new DataPoint(0, 0)
};
return dp;
}

Related

How can I shuffle an array and put them into buffered images?

Basically I'm making a domino game in Java; however I'm pretty new and can't figure out how to shuffle the dominoes up using arrays and buffered Images.
This code here is setting up the arrays for each hand and an array with each domino piece. Keep in mind that I am not using an AI for this domino game;
the program runner will just play as each player.
static String [] hand1 = new String[7];
static String [] hand2 = new String[7];
static String [] hand3 = new String[7];
static String [] hand4 = new String[7];
static String[] allDominoes = {"00","01","02","03","04","05","06","11","12","13","14","15","16","22","23",
"24","25","26","33","34","35","36","44","45","46","55","56","66"};
This is what I tried to use to shuffle the dominoes and put them into each hand array:
public static void newRound() // start a new round; shuffle the dominoes; hand them out
{
Arrays.fill(hand1, null);
Arrays.fill(hand2, null);
Arrays.fill(hand3, null);
Arrays.fill(hand4, null);
String[] shuffledDominos = new String[28];
List<String> shuffled = Arrays.asList(allDominoes);
Collections.shuffle(shuffled);
shuffled.toArray(shuffledDominos);
int pos = 0;
for (int j = 0; j < 7; j++)
{
hand1[pos] = shuffledDominos[j];
pos++;
}
pos = 0;
for (int y = 7; y < 14; y++)
{
hand2[pos] = shuffledDominos[y];
pos++;
}
pos = 0;
for (int x = 14; x < 21; x++)
{
hand3[pos] = shuffledDominos[x];
pos++;
}
pos = 0;
for (int z = 21; z < 28; z++)
{
hand4[pos] = shuffledDominos[z];
pos++;
}
However I think this part is completely wrong and there must be a better way of doing this since I'm not sure how to actually proceed from here on out.
This is assigning each domino Image the domino pngs.
public DominoPanel()
{
try
{
dom00 = ImageIO.read(DominoPanel.class.getResource("/Image/00.png"));
dom01 = ImageIO.read(DominoPanel.class.getResource("/Image/01.png"));
dom02 = ImageIO.read(DominoPanel.class.getResource("/Image/02.png"));
dom03 = ImageIO.read(DominoPanel.class.getResource("/Image/03.png"));
dom04 = ImageIO.read(DominoPanel.class.getResource("/Image/04.png"));
dom05 = ImageIO.read(DominoPanel.class.getResource("/Image/05.png"));
dom06 = ImageIO.read(DominoPanel.class.getResource("/Image/06.png"));
dom11 = ImageIO.read(DominoPanel.class.getResource("/Image/11.png"));
dom12 = ImageIO.read(DominoPanel.class.getResource("/Image/12.png"));
dom13 = ImageIO.read(DominoPanel.class.getResource("/Image/13.png"));
dom14 = ImageIO.read(DominoPanel.class.getResource("/Image/14.png"));
dom15 = ImageIO.read(DominoPanel.class.getResource("/Image/15.png"));
dom16 = ImageIO.read(DominoPanel.class.getResource("/Image/16.png"));
dom22 = ImageIO.read(DominoPanel.class.getResource("/Image/22.png"));
dom23 = ImageIO.read(DominoPanel.class.getResource("/Image/23.png"));
dom24 = ImageIO.read(DominoPanel.class.getResource("/Image/24.png"));
dom25 = ImageIO.read(DominoPanel.class.getResource("/Image/25.png"));
dom26 = ImageIO.read(DominoPanel.class.getResource("/Image/26.png"));
dom33 = ImageIO.read(DominoPanel.class.getResource("/Image/33.png"));
dom34 = ImageIO.read(DominoPanel.class.getResource("/Image/34.png"));
dom35 = ImageIO.read(DominoPanel.class.getResource("/Image/35.png"));
dom36 = ImageIO.read(DominoPanel.class.getResource("/Image/36.png"));
dom44 = ImageIO.read(DominoPanel.class.getResource("/Image/44.png"));
dom45 = ImageIO.read(DominoPanel.class.getResource("/Image/45.png"));
dom46 = ImageIO.read(DominoPanel.class.getResource("/Image/46.png"));
dom55 = ImageIO.read(DominoPanel.class.getResource("/Image/55.png"));
dom56 = ImageIO.read(DominoPanel.class.getResource("/Image/56.png"));
dom66 = ImageIO.read(DominoPanel.class.getResource("/Image/66.png"));
}
catch(Exception E)
{
}
I know I probably didn't explain this well so I will try to summarize it here as best as I can. I need to be able to assign a random domino to each player and then print them out in a JPanel using buffered Images.

Java monitors and thread concurrency

I'm trying to build simple multithreading application. But I'm confused about Java monitors. I have many threads that want to format with their data one array. So for example I have Supermarket Threads (data of the thread is in txt file) So first thread have these product (Milk, Cheese, Chocolate) and country code for each product 1,2, 3
SupermarketA
Milk 1
Cheese 2
Chocolate 3
SupermarketB
Yogurt 1
Orangle 2
Bannana 3
Tea 7
Kiwi 9
and I want to format array that has to fields (country_code and count)
So my array should look like that
Country_code count
1 2
2 2
3 2
7 1
9 1
Code
public class SortedArray{
private int num = 0; // num is country code
private int count = 0;
}
So here's my monitor class
public class SingleArray {
private SortedArray[] array;
private int arrayIndex;
private static final int MAX_SIZE = 5;
public SingleArray() {
array = new SortedArray[MAX_SIZE];
arrayIndex = 0;
initArray();
}
private void initArray() {
for (int i = 0; i < MAX_SIZE; i++) {
array[i] = new SortedArray();
}
}
public synchronized void inc(){
awaitUnderMax();
notifyAll();
}
private void awaitUnderMin(){
while (arrayIndex == 0) try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void dec(){
awaitUnderMin();
notifyAll();
}
public void add(ArrayList<Integer> count){
for (int i = 0; i < count.size(); i++) {
singleArray.inc();
int num = count.get(i);
if (singleArray.arrayIndex == 0) { // if array is empty add value to it
singleArray.array[0].num = num;
singleArray.array[0].count++;
singleArray.arrayIndex++;
} else {
if (!isThere(num)) { // if num is a new value to array
singleArray.inc();
int index1 = singleArray.arrayIndex;
if (num > singleArray.array[index1 - 1].num) {
singleArray.inc();
singleArray.array[index1].num = num;
singleArray.inc();
singleArray.array[index1].count++;
singleArray.inc();
singleArray.arrayIndex++;
System.out.println(Thread.currentThread().getName() + " first " + singleArray.array[index1].num);
} else if (num < singleArray.array[index1 - 1].num) { // jei num mazesne uz paskutinia masyvo reiksme
int index = index1 - 1 < 0 ? index1 : index1 - 1;
while (index > 0 && num < singleArray.array[index].num) {
index--;
}
if (index != singleArray.arrayIndex) {
System.out.println(Thread.currentThread().getName() + " sec " + singleArray.array[index].num);
singleArray.array = addPos(singleArray.array, index + 1, num);
}
}
}
}
}
}
public boolean isThere(int number){
for(int i=0; i<singleArray.arrayIndex; i++){
singleArray.inc();
if(number == singleArray.array[i].num){
singleArray.array[i].count++;
return true;
}
}
return false;
}
private void awaitUnderMax(){
while (arrayIndex >= MAX_SIZE) try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void removeValue(int number, int howManyItems){
for(int i=0; i<arrayIndex; i++){
dec();
if(number == array[i].num){
int numberToDelete = array[i].count - howManyItems >= 0 ? howManyItems : array[i].count;
if(array[i].count >= numberToDelete){
array[i].count -= numberToDelete;
}
if(array[i].count == 0){
deleteItem(i);
}
}
if(array[i].count == 0){
deleteItem(i);
}
}
}
Each thread call add(ArrayList<Integer> count) method
So basically what add method does:
Find place where to insert new value (dependng if new value is greater or lower than a previous)
call isThere(int num) method that check if new value is already in array (if so increment count singleArray.array[i].count++) otherwise add new value to array
If array is full arrayIndex == MAX_SIZE wait current thread for other threads to decrement arrayIndex (this is oly one part of code I also have other threads that based on county code decrement array)
So the biggest problem is that multiplethreads need to update single array at the same time (I know that adding synchronized keyword to add method should solve this problem but it only let one thread to run this method at once!) So sometimes all works fine, but sometimes I get really starnge results (for example that country code is 0 (That is imposible!!!) and sometimes new values is placed in wrong array posiitons). Also I think that semaphores should solve this problem, but is it possible to do that with monitors? Thank's for the answers.
EDIT v2
to #Elyasin
public Thread[] setUpShopsBuilderThreads(){
int size = data.getSize();
ArrayList<ArrayList<String>> a = new ArrayList<>();
ArrayList<ArrayList<Integer>> b = new ArrayList<>();
ArrayList<ArrayList<Double>> c = new ArrayList<>();
Thread[] threads = new Thread[size];
for (int i = 0; i < size; i++) {
int tmp = data.getIndex(i);
int range = i + 1 < size ? data.getIndex(i + 1) : data.getWaresSize();
ArrayList<String> name = new ArrayList<>();
ArrayList<Integer> count = new ArrayList<>();
ArrayList<Double> price = new ArrayList<>();
for (int j = tmp; j < range; j++) {
name.add(data.getName(j));
count.add(data.getCount(j));
price.add(data.getPrice(j));
}
a.add(name);
b.add(count);
c.add(price);
}
procesas_1 p1 = new procesas_1(a.get(0), b.get(0), c.get(0));
procesas_2 p2 = new procesas_2(a.get(1), b.get(1), c.get(1));
procesas_3 p3 = new procesas_3(a.get(2), b.get(2), c.get(2));
procesas_4 p4 = new procesas_4(a.get(3), b.get(3), c.get(3));
procesas_5 p5 = new procesas_5(a.get(4), b.get(4), c.get(4));
Thread worker1 = new Thread(p1);
Thread worker2 = new Thread(p2);
Thread worker3 = new Thread(p3);
Thread worker4 = new Thread(p4);
Thread worker5 = new Thread(p5);
threads[0] = worker1;
threads[1] = worker2;
threads[2] = worker3;
threads[3] = worker4;
threads[4] = worker5;
return threads;
}
public static void main(String[] args) {
Starter start = new Starter();
start.read();
start.printShopsData();
start.printUserData();
Thread[] builderThreads = start.setUpShopsBuilderThreads();
for(int i=0; i<builderThreads.length; i++){
builderThreads[i].start();
}
}
what about using the concurrent safe datasets java already provides?
if you want it sorted, this one looks it might work for you:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentSkipListSet.html
just add it as in a normal Collection

Temp arrays not working after adding 3rd Item

I had to make a Temp array to keep resizing the array list if the user decides to keep adding items to the cart, but my Temp array works until I try to add 3 different items to the cart.
I was instructed to do it this way instead of an array list to show the difficulty of arrays.
orderProduct [productCount] = aProduct;
orderQuantity [productCount] = aQuantity;
}
}
You forgot to increase productCount when there is already a product in the cart.
Moreover, you can just set the product and quantity array to the temp arrays instead of copying back.
orderProduct = tempOrderedProducts;
orderQuantity = tempOrderedQuantity;
Because you forgot productCount++ after resizing the array.
The following code will work:
public void setOrderProduct(Product aProduct, int aQuantity) {
if (productCount == 0) {
orderProduct[0] = aProduct;
orderQuantity[0] = aQuantity;
} else {
Product[] tempOrderedProducts = new Product[orderProduct.length + 1];
int[] tempOrderedQuantity = new int[orderQuantity.length + 1];
for (int i = 0; i < orderProduct.length; i++) {
tempOrderedProducts[i] = orderProduct[i];
tempOrderedQuantity[i] = orderQuantity[i];
}
orderProduct = new Product[tempOrderedProducts.length];
orderQuantity = new int[tempOrderedQuantity.length];
for (int i = 0; i < orderQuantity.length; i++) {
orderProduct[i] = tempOrderedProducts[i];
orderQuantity[i] = tempOrderedQuantity[i];
}
orderProduct[productCount] = aProduct;
orderQuantity[productCount] = aQuantity;
productCount++; //you forgot this
}
}
What's more, there is a simple way to deal with array copy:
public void setOrderProduct(Product aProduct, int aQuantity) {
if (productCount == 0) {
orderProduct[0] = aProduct;
orderQuantity[0] = aQuantity;
} else {
Product[] tempOrderedProducts = new Product[orderProduct.length + 1];
int[] tempOrderedQuantity = new int[orderQuantity.length + 1];
//System.arraycopy is more convenient and efficient
System.arraycopy(orderProduct, 0, tempOrderedProducts, 0, orderProduct.length);
System.arraycopy(orderQuantity, 0, tempOrderedQuantity, 0, orderQuantity.length);
//you don't need to copy back, just re-assign the reference
orderProduct = tempOrderedProducts;
orderQuantity = tempOrderedQuantity;
orderProduct[productCount] = aProduct;
orderQuantity[productCount] = aQuantity;
productCount++;
}
}

How to fill an array with while loop?

I've a piece of code something like this and I want to insert the data by using a while.
Weather weather_data[] = new Weather[]{
new Weather(0, "Cloudy"),
new Weather(0, "Showers"),
new Weather(0, "Snow"),
new Weather(0, "Storm"),
new Weather(0, "Sunny")
};
How can I fill this list using a while ?
Thanks
Try this:
Weather weather_data[] = new Weather[5];
int i = 0;
while(i < weather_data.length){
//fill array [i]
i++;
}
I understand that you want to fill your array with the following String array as base:
String[] weatherTypes = {"Cloudy","Showers","Snow","Storm","Sunny"};
Then you can do this:
int i=0;
Weather[] weather_data = new Weather[];
for (String weatherType: weatherTypes){
weather_data[i] = new Weather(0,weatherType);
i++;
}
You could use also use a list:
List<Weather> weather_data = new ArrayList<Weather>();
for (String weatherType: weatherTypes){
weather_data.add(new Weather(0,weatherTypes));
}
Weather[] weather_data = new Weather[5];
public void addWeatherData(Weather newWeather) {
int counter = 0;
while (counter < weather_data.length) {
if (weather_data[counter] == null) {
weather_data[counter] = newWeather;
return;
}
counter++;
}
}

Constructor of Array Point

I'm working on a programm in which I want my object "this" will be an array of Point but I have this error when I run the programm and I'm not understand why.
Error --> DouglasPeucker.
My programm :
public class DouglasPeucker {
private double epsilon;
protected Point[] coinImage;
public DouglasPeucker(Point [] tab) {
this.coinImage = new Point[tab.length];
for(int i = 0; i < this.coinImage.length; i++) {
double abscisse = tab[i].getX();
double ordonnee = tab[i].getY();
System.out.println(abscisse + " " + ordonnee);
this.coinImage[i].setX(abscisse);
this.coinImage[i].setY(ordonnee);
}
}
You're never assigning a value to coinImage[i], so it will have its default value of null, which you're the dereferencing. You need something like:
for(int i = 0; i < this.coinImage.length; i++) {
double abscisse = tab[i].getX();
double ordonnee = tab[i].getY();
System.out.println(abscisse + " " + ordonnee);
this.coinImage[i] = new Point();
this.coinImage[i].setX(abscisse);
this.coinImage[i].setY(ordonnee);
}
Or preferrably, IMO:
for (int i = 0; i < this.coinImage.length; i++) {
// I'm assuming Point has a sensible constructor here...
coinImage[i] = new Point(tab[i].getX(), tab[i].getY());
// Insert the diagnostics back in if you really need to
}

Categories

Resources