0

Nullpointer Exception...Array length is null

https://code.sololearn.com/c18FqAIuF772 Hi, can you guys please help me out by checking what is wrong with my code? When i dump everything into my Theatre class and run, it is able to run. However, i could not initialize 2 theatre at my main method and made it randomly select by the customer.

17th Jul 2022, 9:25 PM
Andrew Yap Yuntze
Andrew Yap Yuntze - avatar
5 Answers
+ 2
Andrew Yap Yuntze you might want to intialize those 2 theatre using setTheatre. for (int i = 0; i < theatreNum; i++) { theatreSeats.setTheatre(); theatres[i] = new Theatre(); theatres[i].setTheatre(); } In run method inside seating , it would be easy to select random theatre now, public void run(){ fromTheatre = rand.nextInt(theatreNum); theatre[fromTheatre].randomReservation(); } } Can pass fromTheatre as an argument to randomReservation as you would know from which theatre customer selected seats. Ps: There are too many theatre class in your code which was quite confusing for me .Anyway , i hope that i was able to help you somehow!
17th Jul 2022, 10:52 PM
Abhay
Abhay - avatar
+ 1
Thank you so much! You really helped me alot! But i have some syntax error where my outputs overlap with one another at the same line, my theatre number cant be randomized as well as the customer cant select 1 to 3 seats randomly. Ive already uploaded my updated code in the link
18th Jul 2022, 8:24 AM
Andrew Yap Yuntze
Andrew Yap Yuntze - avatar
+ 1
Andrew Yap Yuntze may i know what exactly you want to do here ? Like is it supposed to have 200 customers select seats from any 2 theatre hall or something else ? -------------------------------------------------- Also threads should run on same object for output to be properly synchronized. Following code however runs threads on 2 different objects, fromTheatre = rand.nextInt(theatreNum); theatre[fromTheatre].randomReservation();
20th Jul 2022, 4:26 PM
Abhay
Abhay - avatar
0
you are creating a new Theatre class object theatreSeats inside seating class and then running randomReservation method on it without setting the object theatreSeats using setTheatre() ,that is why you get an error "array length is null".
17th Jul 2022, 10:25 PM
Abhay
Abhay - avatar
0
May i know where should i put my setTheatre() pleaseee? Cause if i put it into my run() method, itll reset the theatreHall everytime i run it at my main method
this is because im running a simulation with 200 threads
17th Jul 2022, 10:50 PM
Andrew Yap Yuntze
Andrew Yap Yuntze - avatar