java - How do I fix my scanner input that is null? -


hi new @ java programming , ran problem on latest project. making score generator bowling , finished code, except when went test it, said

"exception in thread "main" java.lang.nullpointerexception @ bowling_score.main(bowling_score.java:34)"

i tried fix it, , looked through tons of websites, no solutions solved problem. it's easy fix, can't seem find answer. line has problem second line here.

    system.out.println("frame 1, throw 1");     frame1throw1 = sc.nextint(); 

this way know how use scanner variables, if there better way please tell me. may problem because variable frame1throw1 first variable on list.

the variable correct, , scanner's name sc

please specific answer, because said, new @ java, , learning basics now. first big project.

thank you!

p.s. use eclipse code don't know if matters or not

* got 1 answer, , helpful, didn't work. here more of beginning of code may helpful in answering.

     import java.util.scanner;     public class bowling_score {       private static scanner sc; public static void main(string[] args) { //all variables     int frame1throw1 = 0;     int frame1throw2 = 0;     int frame2throw1 = 0;     int frame2throw2 = 0;     int frame3throw1 = 0;     int frame3throw2 = 0;     int frame4throw1 = 0;      //then have 1 variable each throw of game, , 1 final total score.         //directions     system.out.println("to put in score, put number of pins  knocked down in throw specified. if strike, enter 10 in first throw of frame , 0 in second throw of frame. if spare, ener 0 in first throw of frame , 10 in second throw of frame.");      //frame 1     system.out.println("frame 1, throw 1");     frame1throw1 = sc.nextint();     if (frame1throw1 == 10){         frame1throw1 = frame1throw1 + frame2throw1 + frame2throw2;         frame1throw2 = 0; } 

a nullpointerexception means object referencing hasn't been initialised. in case imagine sc hasn't been created in code.

look like

scanner sc; 

and change

scanner sc = new scanner(system.in); 

otherwise scope problem (you created object somewhere can't seen method) you'll need provide more code if first solution doesn't work.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -