i am trying to make a loop for all the selected ID, but unfortunately it does not working. only the the 1st Id entered was accepted and do not loop on the next value in the arraylist. here's my code. I don't know where I missed something. Thanks!
if (arraylistSelectedConsumerIds != null)
{
for (int i = 0; i <arraylistSelectedConsumerIds.size(); i++)
ConsumerId = arraylistSelectedConsumerIds.get(i);
databaseAdapter.updateEmailmark(ConsumerId);
Toast.makeText(AdminActivity.this,"consumer id" + ConsumerId, Toast.LENGTH_LONG).show();
}
Looks like a basic syntax error to me.
if (arraylistSelectedConsumerIds != null) {
for (int i = 0; i <arraylistSelectedConsumerIds.size(); i++) {
ConsumerId = arraylistSelectedConsumerIds.get(i);
databaseAdapter.updateEmailmark(ConsumerId);
Toast.makeText(AdminActivity.this,"consumer id" + ConsumerId, Toast.LENGTH_LONG).show();
}
}
Just add the curly braces for the loop like I did here and make sure you use your IDE to check for further syntax errors.
You should to debug it. Maybe arraylistSelectedConsumerIds is linked to databaseAdapter, and size changed when you call updateEmailmark. If that's the case, you should store the value first. For example:
int listSize = arraylistSelectedConsumerIds.size();
for(int i = 0; i <listSize; i++)
Related
I am trying to get the last ID inserted on the Java Derby DataBase.
This is a little program that i did, but i think that could be better ways to do that:
Query queryUsuarios = em.createNamedQuery("Usuario.findAll");
List<Usuario> listUsuario = queryUsuarios.getResultList();
int i=0;
for (i = 0; i < listUsuario.size(); i++) {
Usuario verUsuario = em.find(Usuario.class, i);
if(verUsuario != null) {
System.out.println(i+")"+verUsuario.getNombres());
}
}System.out.println("The last ID is "+i);
The main question is, there is a better and more secure way to do this?. Because i think on this way i could get errors in the future...
Thank you!.
You can do it using LIMIT and order by.Check below:
SELECT * FROM `table_name` ORDER BY `table_id` desc LIMIT 1;
So I am making a program that needs to be overwrite value i from ArrayList to value i in array. For the life of me I cannot figure out what I should do. I've tried looking for similar problems here, but can't seem to find them. Obviously, my loop is very wrong as it is, as it is just overwriting the entire loop, but I can't figure it out. Any kind-hearted person want to help me?
BTW, I am using Java with Processing
Dot[] dots = new Dot[16];
ArrayList<Dot> extraDots = new ArrayList<Dot>();
Fill them with values and later ...
for (int i = 0; i < dots.length; ++i) {
if (dots[i].timeRemain == 0 && !dotTouch)
{
//arrayCopy(extraDots, i, dots, i, 1);
//this is basically what I want, but from an arraylist to the array
dots = extraDots.toArray(new Dot[i]); //So, so wrong, I know
dotTouch = true;
}
dotTouch = false;
You mean
dots[i] = extraDots.get(i);
???
i didn't get your problem..
simply you can do like below to copy from arraylist to array
why you are using dots[i].timeRemain and dotTouch. can you clarify??
for (int i = 0; i < dots.length; ++i) {
dots[i] = extraDots.get(i);
}
I'm trying to make a tetris game for android to help learn game programming for android. My goLeft/Rights break right when the button is pressed, the code for going left is in a class separate of the fields int array, and the list parts array. The fields array is accessed by a referenced variable (TetrisWorld tetrisworld;). While part list array is public so accessed through a variable(part) code for which is in the goLeft() code. It breaks at: if(tetrisworld.fields[x][part.y] != 0) Code for left:
public void goLeft() {
int x = 0;
for(int i = 0; i < 4; i++) {
TetrisParts part = parts.get(i);
x = part.x - 1;
if(tetrisworld.fields[x][part.y] != 0) {
noleft = true;
break;
}
}
if(noleft == false) {
for(int i = 0; i < 4; i++) {
TetrisParts part = parts.get(i);
part.x--;
}
}
}
The code for the fields int array:
int fields[][] = new int[WORLD_WIDTH][WORLD_HEIGHT];
WORLD_WIDTH and WORLD_HEIGHT are both static final ints, width being 9 and height being 19
I've tried putting if(tetrisworld.fields[0][0] == 0) and it still crashes so I don't think it has to do with the variables. Also It doesn't go out of bound even if I haven't added the code to check for that yet because I have the teroid spawning around x = 5 and since I can't go left/right once there's not a chance of that happening
I've tried moving the goLeft/Right methods to the gamescreen class which has a "world = TetrisWorld();" and it still bugs out at the same spot
UPDATE:
Ok just adding:
tetrisworld != null
to the first if statement fixed it, my question now is, why did it fix it? Why can't I move without this check? It clearly isn't null cause as far as I know; it's fully responsive now.
But an easier way to have solved this which is SOOOO easy is changing fields to static... then access it lika so: TetrisWorld.fields so my updated code is:
public void goLeft()
{
noleft = false;
for (int i = 0; i < 4; i++)
{
part = parts.get(i);
if (part.x - 1 < 0 || TetrisWorld.fields[part.x - 1][part.y] != 0)
{
noleft = true;
break;
}
}
if (noleft == false)
{
for (int i = 0; i < 4; i++)
{
part = parts.get(i);
part.x--;
}
}
}
Looks like you are hitting IndexOutOfBoundsException.
When you are doing x = part.x - 1;, your x variable can become lesser tan zero, thus your code will act like if(tetrisworld.fields[-1][part.y] != 0
It looks like you're getting a java.lang.NullPointerException when trying to access the array in tetrisworld. In the line you mention there are several ways that this could occur:
if(tetrisworld.fields[x][part.y] != 0) {
tetrisworld could be null.
The fields member of tetrisworld could be null.
The second array that you're looking up by using tetrisworld.fields[x].
The value of part could be null.
Having a quick look through your source code it looks to me like you never initialise tetrisworld, either at declaration using:
TetrisWorld tetrisworld = new TetrisWorld();
Or at some other point which is certain to have happened before your goLeft() method is called.
Ok I believe I found the answer, referencing: http://en.wikipedia.org/wiki/Null_Object_pattern
Apparently java will throw an NPE if you don't check for it first if you have a null reference? Is there any way to initialize it without doing a TetrisWorld tetrisworld = new TetrisWorld(); because it's already created in a different class so i get a thousand errors, an actual stack overflow! lul... Still not 100% positive. Please comment to verify and possibly suggest a better way to go about this.
I am writing an app for fellow students, and I am parsing information from a website. I paid a guy to write the parser but did a poor job and he wants more money to help anymore. So im trying to fix it myself.
There is the extractor that grabs the xml info, how the gentlemen had it, was he called it in the main.java like this.
Content1 = extractor.BusRoutes.get(1);
Content2 = extractor.BusRoutes.get(2);
Content3 = extractor.BusRoutes.get(3);
But there is 30+ buses, and to me, that is not a solid idea. So I tried to do a while loop and an array in order to build the list and make it a list in android.
public class BusRoutes extends ListActivity {
TransitXMLExtractor extractor;
public String[] busroutearray()
{
extractor = new TransitXMLExtractor();
String[] busroutearray = new String[40];
int n = 0;
while(n != (busroutearray.length - 1)){
busroutearray[n] = extractor.BusRoutes.get(n);
n++;
}
return busroutearray;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, busroutearray()));
getListView().setTextFilterEnabled(true);
}
bus when i launch it, i always get a forced quit.
edit, the n++ was there before, but got deleted while changing up the code, but with the n++ it still has same effect.
You need to increment your counter (n). So the code for the loop would be:
while (n != (busroutearray.length - 1)) {
busroutearray[n] = extractor.BusRoutes.get(n);
n++;
}
Better in this case would be a for loop (a bit easier to read and ideal when you just want to iterate through :
for (int i = 0; n < busroutearray.length; i++) {
busroutearray[i] = extractor.BusRoutes.get(i);
}
You might also want to explore using different data structures that are a bit more flexible than arrays (example).
Problem is in your while loop. It is running an infinite while loop because you doesn't change "n" value.
public String[] busroutearray()
{
extractor = new TransitXMLExtractor();
String[] busroutearray = new String[40];
int n = 0;
for (int n = 0; n < busroutearray.length; n++){
busroutearray[n] = extractor.BusRoutes.get(n);
}
}
You are not incrementing n, it's an infinite loop. And Android prolly threw a ANR (Application not Responding).
int n = 0;
while(n < busroutearray.length){
busroutearray[n] = extractor.BusRoutes.get(n);
n++;
}
Though i'm not sure, I think BusRoutes is Some kind of List of String
Why don't you use,
String[] busroutearray = extractor.BusRoutes.toArray(new String[extractor.BusRoutes.size()]);
I keep receiving a NullPointerException while trying to get a string from any array (that is encapsulated within a Vector). I cannot seem to stop the error from happening. It's got to be something simple, however I think that I have been looking at it for too long and I could sure use another set of eyes. Here is my code:
Vector<Event> details = vector.get(i).getEvent();
for (int x = 0; x < details.size(); x++) {
Event eDetails = details.get(x);
person = eDetails.getEventPerson();
place = eDetails.getEventPlace()[0];
time = eDetails.getEventTime()[0];
}
So when I try to get the item at position 0 in the array (when x is 0) that is returned from eDetails.getEventTime, a NullPointerException is thrown.
Now, when x is 0 I happen to know that the array element at position 0 of the getEventTime() array is an empty string, but it is NOT a null value. When x is 1 or 2, etc. I can retrieve the time just fine.
The problem is that I will still receive the NullPointerException when I try to do things like the following:
**System.out.println(eDetails.getEventTime.length);**
or
String result;
**if(eDetails.getEventTime[0] == null){**
result = "";
} else {
result = eDetails.getEventTime[0];
}
Any ideas?
Thanks!
Are you sure in your second example, it shouldn't be:
if(eDetails.getEventTime() == null)
Instead of:
if(eDetails.getEventTime[0] == null)
Are you making sure you leave the [0] off when you do the null check?
If the function eDetails.getEventTime() returns null, then you'll get a NullPointerException when you try to do eDetails.getEventTime()[0];
Seems that when you get details.get(0).getEventTime() the array returned is null.
The simplest way to figure this out is:
Vector<Event> details = vector.get(i).getEvent();
for (int x = 0; x < details.size(); x++) {
Event eDetails = details.get(x);
if (eDetails == null) {
throw new NullPointerException("eDetails on pos " + x + " is null");
}
person = eDetails.getEventPerson();
Something[] places = Details.getEventPlace();
if (places == null) {
throw ....
}
place = eDetails.getEventPlace()[0];
Something[] times = eDetails.getEventTime();
if (times == null) {
throw ....
}
time = eDetails.getEventTime()[0];
}
It may not look nice but at least it's informative.