• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayIndexOutOfBounds

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm stuck in my criteriaFind() method.
I'm done with parsing...the following method creates Map of Field/Value from db.db with Fields provided by the user.
Here is the code:

[CODE}
private Map fieldValueMap(DataInfo dataInfoRecord, Set criteriaMapField) throws IOException, DatabaseException{
Map matchingRecordCriteriaMap = new HashMap();
FieldInfo [] currentField;
String currentFieldName;
currentField = dataInfoRecord.getFields();
for ( int j=1; j<=currentField.length; j++) {
currentFieldName = currentField[j].getName();
if ( criteriaMapField.contains(currentFieldName)) {
matchingRecordCriteriaMap.put(currentFieldName,dataInfoRecord.getValues()[j]);
}
}

return matchingRecordCriteriaMap;
}
[/CODE]
At line :
currentFieldName = currentField[j].getName();
I'm getting ArrayIndexOutOfBoundsException. But for me everything looks fine. Can anybodyu point out my mistake?
Panku
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Panku,
I think your problem is:

I think Sun deliberately confused the assignment by making a loop over the records count from 1 up to and including the final record number.
But once you have got around this minor weirdness, everything else uses the standard 0 ... length -1 form.
So stepping through the fields within a record, you should start with field zero (at the moment you miss a field) and stop before you hit the length (which causes your array out of bounds).
Regards, Andrew
 
Panku Panchal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Andrew,
Its done....even I remeber I had tried it once but it didn't work and when you told me and I tried program complied in a moment....MIDAS touch!!! though I found out other errors due to removal of arrayindexoutofbounds.
My set comparision won't work....it will always return null and so calling method will report error. What will be wrong in my usage?
Am I correct in using the following method?
boolean contains( Object o)
If set contains Object o then this method will return true right? I'm comparing the same but still when I give "Destination='SFO'" it doesn't match any record and returns null.
I wonder what mistake I might be doing?
Please do comment on this.
Thank You,
Panku
 
Panku Panchal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,
Its working now for String values of search like
"Desitnation='LAX' or
"Origin='SFO'" or combination of both or Day ...but my program doesn't work for Price=99
or Duration =30m
I'm comparing string I guess,so 99 will also convert to string..then why not result?
Panku
 
Panku Panchal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...working!!
I used trim() to pass the values!!!
Panku
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Panku,
Sorry, yesterday I only looked at your code for coding errors - I did not try to work out the logic of what your program is doing.
As I understand it, you have (externally to the code you posted) created a DataInfo to hold the user's specified column names and criteria to match.
The code you posted then creates a new HashMap containing the criteria where a column name matches the db.db column name, and the value to match in that column.
If I have misunderstood that, then please let me know.
This means that if a column name is mistyped, it will be ignored.
This is contrary to my instructions which state

In the event of an invalid field name being provided as part of the criteria the behavior of this method is the same as if no records matched correctly specified criteria.


Ignoring that, I cannot see how Desitnation='LAX' worked for you - it should have failed because "Destination" is not the correct column name - it should be "Destination airport" - or do we have different databases?
I cannot see any reason why the "Price" or "Duration" searches are failing (but then as I said, I cant se why "Desination" is working). The problem could be either side of this block of code - in the code which converts from the "<criteria>=<value>" string to the DataInfoRecord, or in the criteriaFind method which is processing the map you return back.
If your logic is correct compared to what I've said above, I suggest the next step should be to verify the values that you are passing into this method, and the values you are returning from it. That will help you to narrow down which method you need to look at next.
By the way, could you please go back and edit your original post in this topic? Your UBB code to start the quote is incorrect - you have a curly brace } instead of a square bracket ] to close the UBB, which is why it hasnt formatted correctly. This may put other people off reading this thread.
Regards, Andrew
[ May 01, 2003: Message edited by: Andrew Monkhouse ]
 
Panku Panchal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,
Sorry to confuse you...I was lazy to type the whole field name so I just copy pasted it from my assignment. What you are telling is true. I'm matching user input like
"Destination airport='SFO',Origin airport='LAX'"
with the db.db records and now applying trim() it is working for Price, Time etc other fields.
Here is the corrected code:


Thank You for everything, can you plase tell me now when i'm done with my criteriaFind() method, what should be my next step? To tell you, my criteriaFind() code with all sub-methods will not be more than 60. I've not included any exception in that either. I did not find necessary. What I'm thinking is , if records doen't match then my calling program will report "No matching records found" exception. What do you say?
Also do tell me about my next step....shall I start with client or lock/unlock...confused!!!
Do comment .
Panku
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Panku,

I've not included any exception in that either. I did not find necessary. What I'm thinking is , if records doen't match then my calling program will report "No matching records found" exception. What do you say?


That is certainly an option, but it does mean you have to think about how to pass the exception back through the network interface.
Would it be easier to return an empty set of records and have code on the client side handle what to tell the user if it gets an empty set?
This is completely your decision: whatever you feel comfortable coding and defending in your documentation.
You are making notes every time you ask one of these questions (even to yourself) so you can write up your documentation arent you?


Also do tell me about my next step....shall I start with client or lock/unlock...confused!!!


What do you feel comfortable doing?
Doing the GUI now is very rewarding: you get to see something on screen whereas you can write for ages getting locking / server side working and have nothing to show if someone asks how is your assignment going.
An alternative viewpoint is that you are currently thinking in terms of the database, so you could continue with the remainder of the database coding (lock & unlock).
You need to be aware that your network choice could have an impact on your locking, so if you aren't comfortable with the networking side of it, and how you are going to implement it, then you may want to leave locking until you are clearer about your networking concepts.
Regards, Andrew
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pallavi,
Why are you using trim() in fieldValueMap() method?
While reading records from db, you should trim the records. This is the better way to do, I feel. Also, before saving data into db, you should pad the spaces to top-up the maximum field length. This way you can avoid some mistakes. This is only a suggestion, and you are the better judge for your code.
Better allow dirty reads in criteriaFind, and readRecord/readAllRecords, which simplify your lock/unlock functionality to some extent.
Regards,
Ganapathy
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganapathy,
This is why new assignments get an "NX" in their headers: some things are different between the old and new assignments.
Panku is doing the older FBNS assignment, in which Sun provided us with the code to do the read and write from the database with the admonision that the code is "complete" with the exception of the "lock", "unlock", and "criteriaFind" methods - hence most people are not changing the Sun provided code (except sometimes for fixing deprecation errors and the like).
The Sun provided methods do not trim the nulls from the end of the strings, so we have to do it.
Regards, Andrew
 
S. Ganapathy
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,
May be you are lucky people who are doing old assignment. In new assignemnt, they have not given any initial code. And we ppl are struggling.

Ganapathy
 
Panku Panchal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganapathy,
Yes, as Andrew said, I'm doing old assignment and we have given startup scripts. Those are really useful.
Andrew, thank you for your suggestions.
I want to save GUI last...and as you said to proceed for lock/unlock you should have clear picture of your server side. As I've never worked on RMI I have limited knowldge of it. Can you tell me in short how should I start with client side lock/unlock. To my understanding whenever updates occur on client side I've to lock the record but other client who is accessing the same record should not see the change, am I correct?
i.e. even if one client try to book one seat but he has not confirmed it , other client won't see reduction in available seats, right?
Only server side changes will be reflected in user search.
Panku :roll:
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Panku,
There are lots of posts in this forum about how to do locking, and long talks about whether locking is done:
  • completely in the Data class
  • in a locking manager called from the Data class
  • in an extension of the Data class
  • at all !


  • I think it would be wrong of me to give you a starting point on how to handle locks, as I would naturally be describing what I think is best, and there are plenty of people in this forum who have very valid reasons for suggesting alternatives.
    I suggest you start by looking at some of the threads that already discuss locking.
    Here are some examples, I am sure you can find more:
    this very long thread which is still active - this is a huge thread (3 pages and groiwing) it sometimes goes off topic, but it covers some good points, and has some starting code for you to look at (do not use any of the code "as it stands" since reading the posts will show you the problems with the posted code).
    a topic about a single client attempting multiple locks
    discussion on unique client id for locking records
    possible use for -1 lock
    Regards, Andrew
    [ May 02, 2003: Message edited by: Andrew Monkhouse ]
     
    S. Ganapathy
    Ranch Hand
    Posts: 194
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Pallavi,
    Please go through those links which Andrew mentioned. They are good. But I personally felt that the one which is very long thread, I got bored while reading, ofcourse there were good points in it.
    What Ifeel about locking is there need not be any locking mechanism in single user mode(standalone mode).
    In network mode(RMI/Sockets), there must be locking mechanism.
    synchronized(this) {
        lock(recordNo);
        update(recordNo)/delete(recordNo)/...
        unlock(recordNo);
    }
    This is the style of locking mechanism. synchronized block acts as an atomic operation.
    While locking the record, it will be stored in a datastructure like synchronized list, or synchronized Map. May be in old assignment, synchronized list is enough as per the methods provided in the assignment. But some people used Map where key and value paris are stored. I feel this is not needed, but implementers are the better judges for their desing.
    during locking process, we put an entry in the datastructure. so who ever is accessing the db for modifi9cation, they will wait till that record is unlocked by givingup CPU time. There is no need for the server to really know which client is using that. Server is giving token for the request, and as soon as the request is processed, server takesback the token(token==lock here).
    Here I would like to mention one quote: "Debit what comes-in, and Credit what goes-out regardless of the customer". This is what the real business of the server.
    Implementation of lock/unlock is given in lock/unlock review as mentioned by Andrew.
    To me, better provide the locking functionality in the server, but I really don't know ur requirements.
    Dirty reads are allowed, I feel. If not just mention that "dirty reads are allowed" in the design decisions.
    While booking, say there are 50 seats. And 49 seats were booked already. There is only one seat left. I started booking the seat, and meanwhile u too found one seat is available. I booked it first, and modified the availableSeats variable to 0. Server should check for available seats before booking ur seat. So it will fail, SeatNotAvailableException. I feel the variable seatsAvailable/seatsBookedSoFar should be volatile. Please check for that. In a multi threaded environment, each thread maintains a copy of the member variable if that variable is not volatile. If volatile, even if it has copy, always theread will check the current value with the member variable, as you know.
    Good luck buddy
    Ganapathy
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Ganapathy,
    This email is just to give alternate viewpoints (there is no one right way to do this assignment).
    You suggested: What Ifeel about locking is there need not be any locking mechanism in single user mode(standalone mode).
    The way I see it, the deliverable is broken into three major sections:
    Client GUI <--> Interface <--> Database
    The interface could be either direct connection (local mode) or networked.
    Looking at the instructions for the old FBNS assignment, we have:

    The remote client code that you write must provide all the public methods of the suncertify.db.Data class.


    I have taken that to mean that at least the client side of the network must have access to remote lock and unlock methods (whether it uses them, and what they do is up for discussion ).
    It would be possible to still have the GUI unaware of the locking mechanism, however I believe that for future extensibility the GUI should lock records itself.
    To give you an example of why, consider a two part flight: one flight leaving Holland at 7am arriving in London at 7.30am, to catch the 8.30am flight from London to Sydney. If I cannot get the Holland - London flight, then I dont want the London - Sydney flight. Likewise if I cannot get the London-Sydney flight then I dont want the Holland-London flight. Due to the limited flights from London to Sydney, it is a case of getting both these flights or nothing.
    Now, if I can lock records on the client side, then I can lock the Holland - London flight, check that there is a seat available, lock the London - Sydney flight, check that there is a seat available, book both flights, and unlock them both.
    Without this locking mechanism, I would have to have a method of reversing the entries. Quite often you will find that doing this will generate a lot more network traffic than my "locking" solution, depending on audit requirements may require more data to be saved on the far server, and depending on authorization requirements may require manager intervention. All of which adds up to I would rather be able to lock records and never update than to update and then back out changes.
    I know - the specs dont require anything like this. However the specs do say:

    Your user interface should be designed with the expectation of future functionality enhancements


    So I want to have locking client side - that is my design decision no one else has to follow it
    Going further with your comments, you have the lock - update - unlock in a synchronized block. I cannot see any need for that. The locking mechanism is almost certainly going to be synchronized, and once we have a lock the record is ours - we no longer need to be synchronized.
    You also said: some people used Map where key and value paris are stored. I feel this is not needed People have done this because of the old assignment requirement:

    If an attempt is made to unlock a record that has not been locked by this connection, then no action is be taken.


    Some people have tried to keep track of which client locked the record to meet this requirement, hence the Map. I am not commenting on whether I agree or not
    I do not think your suggestion of a volatile variable for tracking how many seats have been booked is viable. Are you keeping a variable for evey flight that may be booked? That could be a lot of variables. Plus it is then making the database specific to the current implementation of flights availability. This is wrong on two counts: if the requirments change (e.g. having different numbers of business class versus normal class to be booked) then you would have to rethink the entire booking concept. More importantly, the database is no longer a generic database, but specifically tied to the data it is storing.
    Regards, Andrew
    [ May 04, 2003: Message edited by: Andrew Monkhouse ]
     
    Panku Panchal
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Ganpathy and Andrew,
    Thank You for your help...I'll go through all the threads you have provided.
    Ganpathy, Pallavi is my friend and we both are doing the assignment. So once posting reply by mistake she used my name ....we two are different persons :-)....I won't mind you calling me by her name as she is my good friend.
    Anyways...was lazy to work on weekends...will start working from tomorrow on assignment.
    See ya...
    Panku
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic