Ruben Ochoa

Greenhorn
+ Follow
since Nov 11, 2018
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ruben Ochoa

Sorry about that. Line 5 was the error and indeed the error was not there.
My project requirements is to use Menuitem. Lines 6,7,8 also gets error but its not that as you said.
5 years ago
I want to delete record from a file when I press delete from Menuitem at my program.
For doing that I am using ArrayList. And that is my code:
I hope I gave full details.





I get errors from console:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Main.action(Main.java:169)
at java.awt.Component.handleEvent(Unknown Source)
at java.awt.Window.postEvent(Unknown Source)
at java.awt.MenuComponent.postEvent(Unknown Source)
at java.awt.MenuComponent.postEvent(Unknown Source)
at java.awt.MenuComponent.postEvent(Unknown Source)
at java.awt.MenuComponent.dispatchEventImpl(Unknown Source)
at java.awt.MenuComponent.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
5 years ago

Campbell Ritchie wrote:

Ruben Ochoa wrote:. . . Right now i did the output will be only in one line: somename somesurname sometelephone  . . .

Yes, but what is the problem? If you want to change that format, alter the toString() method in the contact class.
Don't close your buffered writer; use try with resources which is a more reliable way to close a resource.



The problem is that i want to add names,surnames,telephones everytime but now the code use the first 3 lines as size.
5 years ago
My program is about contacts. When a Doctor for an example insert name,surname,telephone, everytime he wants to save the data into txt file then output will be:

somename
somesurname
sometelephone

somename
somesurname
sometelephone
...
Right now i did the output will be only in one line: somename somesurname sometelephone as you can see at the code:

if(text.equals("Save")) {              
   try {      
       ArrayList<String> contactsinformations=new ArrayList<>();
       String name=tname.getText();
       String surname=tsurname.getText();
       String telephone=ttelephone.getText();
       contactsinformations.add(0,name+" ");
       contactsinformations.add(1,surname+" ");
       contactsinformations.add(2,telephone+" ");  

       FileWriter outFile = new FileWriter("Contacts.txt");
       BufferedWriter outStream = new BufferedWriter(outFile);                  
       for(int i=0; i<contactsinformations.size(); i++)
           outStream.write(String.valueOf(contactsinformations.get(i)));                
       outStream.close();
       JOptionPane.showMessageDialog(this,"Data saved.");  

   } catch(IOException e) {
       System.out.println("ERROR IN FILE");
   }
}
I use for loop to get the size of the ArrayList but trying to figure out how can I insert the informations in different line.
5 years ago

Knute Snortum wrote:It's good to remember that saying ItDoesntWorkIsUseless (that's a link).  Tell us what happened in detail.  We can't help it we don't know what's going on.



I understand
5 years ago
I tried nane ArrayList into Contacts that i have create but not work.
5 years ago

Knute Snortum wrote:...but weren't we talking about fixing this code?
Then you could do this with a "for each" loop:
That would give you a file with each field on a separate line.  That may not be what you want.

While I write this, I see you have posted how you want the data to be saved.  You probably don't want to use the method above.  Do you have a Contact class?  If so, use a list of contacts, List<Contact>, then use a loop like this:
If you write a toString() method correctly, this format will post to the text file.




Indeed but i wanted to give more informations so will be more cleared.
5 years ago

Carey Brown wrote:What format do you want the line in? How are you going to deal with multiple contacts if you have only one text field for each contact field?



The program I want to create is about a menu that have submenus of Contacts, Dates and Help. At Contact menu I want: 1. Create new contact with name, surname and telephone. 2.Read the file with contacts. Contacts saves in a array. 3.Getting contacts in a file. 4.Delete contacts and 5.Update contacts.
I created 4 textfields. 1.Name 2.Surname.3Telephone4.Date
5 years ago
When a doctor for an example wants to creates contacts of his clients, inserts one by one with separate lines the contacts.
5 years ago

Knute Snortum wrote:Have you tried a "for each" loop?  They work well with Lists:



You mean:
The output is just []
5 years ago

Carey Brown wrote:

Ruben Ochoa wrote:Indeed. The output is [ ],[ ].[ ] 3 times, filled by names. What loop i must use? While(true)? I tried

You need to be clearer when responding. Do you get "[]" or do you get "somename"?

You are filling your array with three pieces of data. When you are printing the data you will get a single line of three pieces of data separated with commas. If you want multiple name entries you'll have to fill your list outside of this write method. You don't need any loop to write the three pieces of data. You will need a loop if you fill in the list outside of your method.



You have right.
The output is: [ somename ],[  somename ], [  somename ]
I used for or while but is pointless
5 years ago
Indeed. The output is [ ],[ ].[ ] 3 times, filled by names. What loop i must use? While(true)? I tried
5 years ago
Hi again everyone.
My problem there is i want to insert contacts by line. For en example:
todd fox 2222222
tod sss 35464564576
jeff star 45252
I used append and everything but is pointless



5 years ago