• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Splitting List<Integer> type to 2 Integers and 2 Integer[]

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a List of Integer Type I wish to break down. The first and last element to individual Integers and elements 2,5,8,11.... to an Integer [] and elements 3,6,9,12... to another Integer []

the following is a print of the initial List, error and code, any help would be appreciated, sure its something simple, just can't seem to figure it out

[20, 1, 91, 29, 2, 60, 65, 3, 61, 71, 4, 9, 60, 5, 79, 45, 6, 46, 71, 7, 19, 22, 8, 57, 97, 9, 8, 6, 10, 84, 91, 11, 20, 57, 12, 72, 60, 13, 32, 49, 14, 31, 89, 15, 28, 2, 16, 81, 30, 17, 55, 90, 18, 43, 25, 19, 100, 82, 20, 27, 19, 524]
Exception in thread "main"
java.lang.NullPointerException
at Items.main(Items.java:35)

 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I think I'm going to need to see the rest of the code. Can you post it here?
 
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

So which line are you getting the Exception on? The line numbers you have posted and the line number on the stack trace are different. I hope that doesn't mean you have a 35‑line main method.
 
Stephen Murphyy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The complete code
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lists are numbered starting with 0.
So you want to extract an element with index 0 to an int variable.
Then you want to extract an element at the end of the list to another int variable.
Then for the rest of the list you want to extract elements with indexes 2, 5, 8, 11... etc to an int array.
Then for you want to extract elements with indexes 3, 6, 9, 12... etc to another int array.
Am I correct to assume you want to ignore elements with indexes 1, 4, 7, 10... ?
 
Campbell Ritchie
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by expunging the keyword static from your vocabulary. You may need it before the main method but avoid it everywhere else.

Then remember to initialise all fields. You did not initialise the arrays anywhere.
 
Stephen Murphyy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I want to ignore those elements.
Thanks for the Launcher Link, no more oversized main
Initialise and drop the static, thanks will do.
 
Campbell Ritchie
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can iterate your List with its index and add the elements to another List/array/whatever depending on whether the index divides by 3.
 
Stephen Murphyy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help, tidied my code up, all working.
 
Campbell Ritchie
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be very careful about
(size() - 2) / 3
That can go horriblly wrong, producing an array 1 too small, if you start from 61 elements.

I have often seen parallel arrays and inveighed against them. I think this exercise shows how you can get even more confusion by trying to put your parallel arrays into one array/list.

By the way: doesn't the first element in the array tell you how many pairs to expect? That might be another bit of error‑prone design like parallel arrays.
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic