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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Binary search to add elements into array throws null pointer exception

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks for the input guys. I've made two seperate methods to remove entries by name or extension number respectively and I'm now working on another part of my project, the graphical user interface. I've made some buttons, a jtextarea and jtextfield. I want to begin by getting the Print Directory button to work. As you know, my printDir() method in the ArrayDirectory class does system.out.println for each of the entries within the array. I'm wondering how I could get the button to call this method but instead of printing the directory to the console, print it on to the JTextArea instead. Here is the code for what I have so far:


Any hints?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Daniel Perera wrote:Any hints?


my suggestion would be to re-post the above code without that really long line so that it is easier for folks to read what you have.
 
Daniel Perera
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:

Daniel Perera wrote:Any hints?


my suggestion would be to re-post the above code without that really long line so that it is easier for folks to read what you have.


Okay, I've modified it so that the the long text is not there. Anyone have any hits as to how I can get the Print Directory button working so that the printDir() method with the system.out.println for all entries is called and the entries are "printed" onto the JTextArea on my GUI instead of the console in Eclipse, as per my previous question? Thanks in advance!
 
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
First change the printDir() method so that it returns a String which contains the desired information. You might also want to rename it, since it no longer prints a directory. In fact you might want to get rid of it entirely because writing to System.out is useless in a GUI application. Anyway getting back to the main idea: Change printDir() to return the string you want to see in your GUI. Then call the setText() method of the text component where you want that string to appear, passing the string.
 
Daniel Perera
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Paul Clapham wrote:First change the printDir() method so that it returns a String which contains the desired information. You might also want to rename it, since it no longer prints a directory. In fact you might want to get rid of it entirely because writing to System.out is useless in a GUI application. Anyway getting back to the main idea: Change printDir() to return the string you want to see in your GUI. Then call the setText() method of the text component where you want that string to appear, passing the string.


I can't change the code as it is how it should be for the first part of my project, and in the GUI part I am supposed to use the methods in the first part, so I need to make it work with the system.out.println line...
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Daniel Perera wrote:

Paul Clapham wrote:First change the printDir() method so that it returns a String which contains the desired information. You might also want to rename it, since it no longer prints a directory. In fact you might want to get rid of it entirely because writing to System.out is useless in a GUI application. Anyway getting back to the main idea: Change printDir() to return the string you want to see in your GUI. Then call the setText() method of the text component where you want that string to appear, passing the string.


I can't change the code as it is how it should be for the first part of my project, and in the GUI part I am supposed to use the methods in the first part, so I need to make it work with the system.out.println line...



So, you're required to have a method that prints some stuff out to the console, even though it's a GUI app? And you're supposed to somehow also use that method to update the GUI? If that's how you interpret your assignment, I cannot suggest strongly enough that you go back and get clarification, because that makes no sense at all. It makes less than zero sense. It is a net sense drain on the universe.

If that's not how you interpret your requirements, can you explain more clearly what they actually are, and what in particular you're having trouble with?
 
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:
  • Report post to moderator

Jeff Verdegan wrote:

Daniel Perera wrote:I can't change the code as it is how it should be for the first part of my project, and in the GUI part I am supposed to use the methods in the first part, so I need to make it work with the system.out.println line...



So, you're required to have a method that prints some stuff out to the console, even though it's a GUI app? And you're supposed to somehow also use that method to update the GUI? If that's how you interpret your assignment, I cannot suggest strongly enough that you go back and get clarification ...


Agreed. This looks very wrong.

Daniel Perera wrote:As you know, my printDir() method in the ArrayDirectory class does system.out.println for each of the entries within the array. I'm wondering how I could get the button to call this method but instead of printing the directory to the console, print it on to the JTextArea instead. [...] Any hints?



Not without some changes - possibly an overloaded method so that the bulk of your code remains the same.

While most people use constructs such as System.out.println() as though it is an indivisible statement, it is worth remembering that System.out is just a field of type PrintStream, and as such, could be passed into a method. Which would then allow some other PrintStream to be passed into the method.

Are those two hints too obscure?
 
Daniel Perera
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jeff Verdegan wrote:

Daniel Perera wrote:

Paul Clapham wrote:First change the printDir() method so that it returns a String which contains the desired information. You might also want to rename it, since it no longer prints a directory. In fact you might want to get rid of it entirely because writing to System.out is useless in a GUI application. Anyway getting back to the main idea: Change printDir() to return the string you want to see in your GUI. Then call the setText() method of the text component where you want that string to appear, passing the string.


I can't change the code as it is how it should be for the first part of my project, and in the GUI part I am supposed to use the methods in the first part, so I need to make it work with the system.out.println line...



So, you're required to have a method that prints some stuff out to the console, even though it's a GUI app? And you're supposed to somehow also use that method to update the GUI? If that's how you interpret your assignment, I cannot suggest strongly enough that you go back and get clarification, because that makes no sense at all. It makes less than zero sense. It is a net sense drain on the universe.

If that's not how you interpret your requirements, can you explain more clearly what they actually are, and what in particular you're having trouble with?


Okay, I must have misinterpreted the project. For part 1, we don't need a GUI, but I suppose I can just have my PrintDir() method return a string instead of having a print line. I will have a go at that now. I'll write back if I have any trouble..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Daniel Perera wrote:
Okay, I must have misinterpreted the project. For part 1, we don't need a GUI, but I suppose I can just have my PrintDir() method return a string instead of having a print line. I will have a go at that now. I'll write back if I have any trouble..



Okay, so if you're changing it to just return a String rather than printing it out, you should also change the name. It doesn't make sense to call something "printDir" that doesn't actually print anything. You could of course still define a method called "printDir" that first calls the method that generates the directory info as a String, and then prints out the results of that method.
 
Daniel Perera
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jeff Verdegan wrote:

Daniel Perera wrote:
Okay, I must have misinterpreted the project. For part 1, we don't need a GUI, but I suppose I can just have my PrintDir() method return a string instead of having a print line. I will have a go at that now. I'll write back if I have any trouble..



Okay, so if you're changing it to just return a String rather than printing it out, you should also change the name. It doesn't make sense to call something "printDir" that doesn't actually print anything. You could of course still define a method called "printDir" that first calls the method that generates the directory info as a String, and then prints out the results of that method.


Yeah, I decided to just make a toString() method in the end and do system.out.println(directory) in the test class. Works fine. I also decided to leave the GUI until later as the second part of the project is apparently easier and worth more marks. So initially in part 1 we were asked to use an array to do all of this, then in part 2 we are asked to

a) Use a LinkedList to achieve the same tasks - completed this part
b) Use hashing to store all of the records in a bunch of lists - e.g. for people with surname starting with an A, put entries in first list, for B in the second list and so on.I need help with this

My deadline is on Friday and I just want to finish this part as well, then I will be able to get around 25/30 marks, which I would be quite happy with :) I've begun by doing the following:

However I'm not able to get it adding the entries and working, I have get an ArrayIndexOutOfBounds exception when I try to initialise the directory in my test class which points to lines 18 and 37. Also on line 7 I am getting a warning that says: "Type safety: The expression of type LinkedList[] needs unchecked conversion to conform to List<Entry>[]. Any help will be appreciated, thanks in advance :)
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Do you know what means? You are declaring an array hashDirectory whose component type is a List of Entry type objects i.e. :


This is not a List.


a) Use a LinkedList to achieve the same tasks - completed this part



Can you show me that code?
 
Daniel Perera
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Mansukhdeep Thind wrote:Do you know what means? You are declaring an array hashDirectory whose component type is a List of Entry type objects i.e. :


This is not a List.


Okay, I saw this from a friend and he said it worked for him so I thought it was the right way to do it, guess not What is the correct way to do what I am trying to achieve - hashing so that I have 26 seperate lists for each letter of the alphabet to store the corresponding entries inside and be able to access as needed?

Mansukhdeep Thind wrote:

a) Use a LinkedList to achieve the same tasks - completed this part



Can you show me that code?


Sure, here it is:

Looking forward to your reply, 24 hours til submission of the project so I have to get this finished asap, I think if you help me with getting the HashDirectory class underway I will be able to complete the rest
 
Marshal
Posts: 80071
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please restrict the length of your lines; long lines are very difficult to read.
 
Daniel Perera
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Campbell Ritchie wrote:Please restrict the length of your lines; long lines are very difficult to read.


He wanted to see the code for the 2nd part of my project - how can I restrict this code? Fair enough if he wanted a specific part but he wanted to see all of it I believe...
 
Campbell Ritchie
Marshal
Posts: 80071
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You divide long lines into several short lines. There is an example of how to do it here.
 
Daniel Perera
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Campbell Ritchie wrote:You divide long lines into several short lines. There is an example of how to do it here.


I don't really think my lines are that long, but I will pay closer attention to them next time.
 
Campbell Ritchie
Marshal
Posts: 80071
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Your lines most certainly were too long. Particularly the //comments, which should have been written as /*...*/ comments.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Never follow blindly what someone else is doing. Just because it works doesn't mean it is correct. You have stated that your assignment calls specifically for using a List to complete this task.
Isn't that so Daniel? And you ended up creating an array of List<Entry>? Aren't you supposed to use List for both line items a and b stated above?


My understanding is that you need to develop a List as follows:



and each of these Lists has to be hashed to a value respective to the starting alphabet of the surnames that it has. Am I interpreting your requirement correctly Daniel?

Campbell / Jeff / Andrew: Please pitch in and provide your inputs.

EDIT: I am unable to keep the posts short. When I quote Daniel's post, it becomes long.







 
Daniel Perera
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Mansukhdeep Thind wrote:Never follow blindly what someone else is doing. Just because it works doesn't mean it is correct. You have stated that your assignment calls specifically for using a List to complete this task.
Isn't that so Daniel? And you ended up creating an array of List<Entry>? Aren't you supposed to use List for both line items a and b stated above?


My understanding is that you need to develop a List as follows:



and each of these Lists has to be hashed to a value respective to the starting alphabet of the surnames that it has. Am I interpreting your requirement correctly Daniel?

Campbell / Jeff / Andrew: Please pitch in and provide your inputs.

EDIT: I am unable to keep the posts short. When I quote Daniel's post, it becomes long.


Yes, 26 seperate lists for surnames starting with A, B, C..... all the way to Z. But the lists need to be created according to my initialiseDirectory() method which reads in the txt file containing the entries, so for example it might make sense to not create a list for surnames starting with A until a surname that starts with A is found - is this hard to achieve?

Thanks
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Daniel wrote: so for example it might make sense to not create a list for surnames starting with A until a surname that starts with A is found - is this hard to achieve?



The input file that you shared with me has surnames starting with all the alphabets, starting from A right up until Z, for sure. It is imperative that there has to be a List for each one, unless of course you have a use case in which you have an input file with surnames starting with only some alphabets.
 
Daniel Perera
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Mansukhdeep Thind wrote:

Daniel wrote: so for example it might make sense to not create a list for surnames starting with A until a surname that starts with A is found - is this hard to achieve?



The input file that you shared with me has surnames starting with all the alphabets, starting from A right up until Z, for sure. It is imperative that there has to be a List for each one, unless of course you have a use case in which you have an input file with surnames starting with only some alphabets.


Fair enough, so how will I ensure that the entries are added onto the correct lists? Got to finish this tonight, I'm not expecting you to write the code for me of course but if you could please give me a bit more to work with I would be very thankful.
 
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:
  • Report post to moderator
Have you looked at Character.getNumericValue(char)? Can you see how that might help?
 
Paul Clapham
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

John Pickles wrote:Fair enough, so how will I ensure that the entries are added onto the correct lists?



Two steps.

1. Get a reference to the correct list.

2. Add the entry to that list.

That will ensure that the entry is added to the correct list. But am I right in guessing that you are actually wondering about how to do the first step?
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    Bookmark Topic Watch Topic
  • New Topic