1

I need to make a game and I need to use textarea. I use libgdx library, but I can't pass String value from one class to other. My program is crashed every time. I guess due to constructers Error: Exception in thread "LWJGL Application" java.lang.NullPointerException at mario.game.Worlds.MainCodeProgram.jump(MainCodeProgram.java:1149)

When i input "jump" error will be in this line: if(zaidejas.b2body.getLinearVelocity().y == 0){zaidejas.b2body.applyLinearImpulse(new Vector2(0, 4f), zaidejas.b2body.getWorldCenter(), true);}

this is my inputs class (where i use textarea)

public class inputs implements Disposable {
    private Skin skin;
    private Viewport viewport;
    public Stage stage;
    private Table table;
    private TextButton button;
    public TextArea TF;

    public inputs(SpriteBatch sb){

        viewport = new FitViewport(1200, 624, new OrthographicCamera());
        stage = new Stage(viewport, sb);

        skin = new Skin(Gdx.files.internal("data/uiskin.json"));
        button = new TextButton("Send", skin, "default");
        TF = new TextArea("", skin);

        TF.setWidth(300);
        TF.setHeight(570);
        TF.setPosition(0,54);

        button.setWidth(300);
        button.setHeight(54);
        button.setPosition(0, 0);

        button.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                MainCodeProgram h = new MainCodeProgram(TF.getText());
                TF.setText("");
                button.setTouchable(Touchable.disabled);
            }
        });
        stage.addActor(TF);
        stage.addActor(button);
        Gdx.input.setInputProcessor(stage);
        Gdx.input.setInputProcessor(stage);
    }
    public void setButtonTouchable(){
        button.setTouchable(Touchable.enabled);
    }

    @Override
    public void dispose() {
        stage.dispose();
    }
}

it is my main code:


public class MainCodeProgram extends JFrame implements Screen, TextInputListener, Disposable {
    static ArrayList<String> firstintkint = new ArrayList<String>();
    static ArrayList<Integer> firstintvalue = new ArrayList<Integer>();  //int
    private Main zaidimas;
    private OrthographicCamera orthographicCamera;
    private TextureAtlas textureAtlas;
    public static int kurislevel=1;
    private Mario zaidejas;
    public Stage stage;
    private Hud hud;
    private TmxMapLoader maploader;   
    private TiledMap tilemap;
    static int walks = 0;
    private OrthogonalTiledMapRenderer renderer;
    private Viewport viewport;
    private World world;
    private Array<Item> items;
    private LinkedBlockingQueue<ItemVector> itemstoSpawn;
    String text="";
    private Box2DDebugRenderer b2dr;
    private OtherWorldCreator otherWorldCreator;
    public  String lose;


public  MainCodeProgram(){

}
    public MainCodeProgram(Main zaidimas,String tg) {
        textureAtlas = new TextureAtlas("All.pack");
        this.zaidimas = zaidimas;
        orthographicCamera = new OrthographicCamera();
        viewport = new FitViewport(Main.Width / Main.PPM, Main.Height / Main.PPM, orthographicCamera); 
        viewport.apply();  //nera
        hud = new Hud(zaidimas.spriteBatch);
        naujaslygis();
        otherWorldCreator = new OtherWorldCreator(this);
        zaidejas = new Mario( this);
        world.setContactListener(new WorldContact());
        items = new Array<Item>();
        itemstoSpawn = new LinkedBlockingQueue<ItemVector>();
    }
    public MainCodeProgram(String text1){
        lose = text1;
        input(lose);
    }
    public TextureAtlas getAtlas(){return textureAtlas;}
    @Override
    public void show() {}
    int i = 0, kur = 1;
    public void spawnItem(ItemVector idef)
    {
        itemstoSpawn.add(idef);
    }
    public void handleSpawningItems(){
        if(!itemstoSpawn.isEmpty())
        {
            ItemVector idef = itemstoSpawn.poll();
            if(idef.type == Mushroom.class){items.add(new Mushroom(this, idef.position.x,idef.position.y));}
        }
    }
    public void update(float dt)
    {
        handleSpawningItems();
        world.step(1 / 60f, 6, 2);
        zaidejas.update(dt);
        for (Enemy enemy: otherWorldCreator.getEbemies()) {
            enemy.update(dt);
            if(enemy.getX() < zaidejas.getX() + 224 / Main.PPM){enemy.b2body.setActive(true);}
        }
        for(Item item : items){item.update(dt);}
        hud.refresh(dt);
        if(zaidejas.Dabartinebusena != Mario.State.mirti){orthographicCamera.position.x = zaidejas.b2body.getPosition().x;}
        orthographicCamera.update();
        renderer.setView(orthographicCamera);
    }
    @Override
    public void render(float delta) {
        update(delta);
        int k = 0;
       Gdx.gl.glClearColor(0,0,0,1);
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        renderer.render();
        b2dr.render(world, orthographicCamera.combined);
        zaidimas.spriteBatch.setProjectionMatrix(orthographicCamera.combined);
        zaidimas.spriteBatch.begin();
        zaidejas.draw(zaidimas.spriteBatch);
        for (Enemy enemy: otherWorldCreator.getEbemies())
            enemy.draw(zaidimas.spriteBatch);
        for(Item item : items)
            item.draw(zaidimas.spriteBatch);
        zaidimas.spriteBatch.end();
        zaidimas.spriteBatch.setProjectionMatrix(hud.stage.getCamera().combined);
        hud.stage.draw();
        if(gameOver())
        {
            kurislevel = 1;
            zaidimas.setScreen(new GameOverWorld(zaidimas));
            dispose();
        }
        if ((zaidejas.getX() > 15 && kurislevel < 4)) {
            kurislevel++;
            if(kurislevel!=4){
                zaidimas.setScreen(new MainCodeProgram(zaidimas,""));
                dispose();
            }
        }
        if (kurislevel==4)
        {
            zaidimas.setScreen(new WinWindow(zaidimas));
            dispose();
        }
        Main.inp1.stage.draw();
    }
    @Override
    public void resize(int width, int height) {viewport.update(width,height);}
    public World getWorld(){ return world;}
    public TiledMap getMap(){return tilemap;}
    public  void naujaslygis()
    {
        maploader = new TmxMapLoader();
        if(kurislevel == 1){tilemap = maploader.load("mario1lvl.tmx");
        }
        if (kurislevel == 2){tilemap = maploader.load("mario2lvl.tmx");}
        if (kurislevel == 3){tilemap = maploader.load("mario3lvl.tmx");}
        if (kurislevel == 4){
            zaidimas.setScreen(new WinWindow(zaidimas));}
            renderer = new OrthogonalTiledMapRenderer(tilemap, 1  / Main.PPM);
            orthographicCamera.position.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2, 0);
        world = new World(new Vector2(0, -10 ), true);
        b2dr = new Box2DDebugRenderer();
    }
    @Override
    public void dispose() {
        tilemap.dispose();
        renderer.dispose();
        world.dispose();
        b2dr.dispose();
        hud.dispose();
    }
    ArrayList<Integer> ifassss = new ArrayList<Integer>();
    public void input(String text) {
        ArrayList<String> pseudokodas = new ArrayList<String>();
        this.text = text;
        i = 0;
        String str = text;
        String split[] = str.split("/");
        for (String spacebar : split) {
            pseudokodas.add(spacebar);
            i = 0;
        }
        int foras = 0;
        for (int i = 0;i < pseudokodas.size(); i++) {
           // System.out.println(pseudokodas.get(i));
            if(pseudokodas.get(i).equals("jump")||pseudokodas.get(i).equals("jump;")){jump();}   
            if(pseudokodas.get(i).startsWith("int")){recognizeint(pseudokodas.get(i));}
            if(pseudokodas.get(i).startsWith("if")){ifas(pseudokodas.get(i),0);}
            if(pseudokodas.get(i).startsWith("for"))
            {
                recognizefor(pseudokodas.get(i));
                foras++;
            }
            if(pseudokodas.get(i).endsWith("--")||pseudokodas.get(i).endsWith("--;"))
            {
                if(foras == 0){minuminus(pseudokodas.get(i));}
                else{foras--;}
            }
            if(pseudokodas.get(i).endsWith("++")||pseudokodas.get(i).endsWith("++;"))
            {
                if(foras==0){plusplus(pseudokodas.get(i));}
                else{foras--;}
            }
            if(istheend){
                walks = 0;
                istheend = false;
            }
        }
    }

    void plusplus(String zodis) {
        if(zodis.startsWith(firstintkint.get(0))){firstintvalue.set(0,  firstintvalue.get(0)+1);}
    }
    void minuminus(String zodis) {
        if(zodis.startsWith(firstintkint.get(0))){firstintvalue.set(0,  firstintvalue.get(0)-1);}
    }
    void recognizeint(String zodis) {
        String convertstrtoint = null;
        String int1 = zodis;
        int reiksme = 0;
        String int2 = "";
        String int3[] = int1.split("[ ;'']");
        for (int c = 0; c < int3.length ; c++) {
            if(int3[c].startsWith("int")) {
                firstintkint.add(int3[c+1]);
                try {reiksme = (Integer.parseInt(int3[c+3]));} catch (NumberFormatException e) {}
                firstintvalue.add(reiksme);
                int2+="int"+" "+int3[c+1]+" "+"="+" "+int3[c+3]+";";
            }
            convertstrtoint = zodis.replace(int2,"");
        }
        input(convertstrtoint);
    }

    public static int isif = 0;
    public static int inforisif=0;
    public static String inifisfor=null;
    public static String inifiswhile=null;
    public static int wasif=0;

    void ifas(String a, int b) {
        boolean int1 = false;
        boolean int2 = false;
        boolean stringarchar=false;
        boolean stringarchar2=false;
        int vykdyk = 0;
        int tolyn = 0;
        String pirmareiksme = null;
        String antrareiksme = null;
        Integer pirmareiksme2 = null;
        double antrareiksme2 = 0;
        isif = b;
        String ifas;
        ifas = a;
        String splitif[] = ifas.split(" ");
        boolean artesti = true;
        for (int l = 0; l < splitif.length; l++) {
            if(splitif[l].equals("1")){
            }
        }
        int kuriojvietoj1=1;
        int kuriojvietoj2=3;
        int kuriojvietojvidurio=2;
        if (isif==0) {
            try {
                pirmareiksme2 = (Integer.parseInt(splitif[kuriojvietoj1]));
            } catch (NumberFormatException e) {
                for (int p = 0; p < firstintkint.size(); p++) {
                    if(splitif[kuriojvietoj1].equals(firstintkint.get(p)))
                    {
                        pirmareiksme2=firstintvalue.get(p);
                        int1=true;
                    }
                }


                if(stringarchar==false&&int1==false){pirmareiksme=splitif[kuriojvietoj1];}
            }
            try {
                antrareiksme2 = (Integer.parseInt(splitif[kuriojvietoj2]));
            } catch (NumberFormatException e) {
                for (int p = 0; p < firstintkint.size(); p++) {
                    if(splitif[kuriojvietoj2].equals(firstintkint.get(p)))
                    {
                        int2=true;
                        antrareiksme2=firstintvalue.get(p);
                    }
                }


            }
        }

        if (isif==1)
        {
            if(inforisif==0){
                kuriojvietoj1+=5;
                kuriojvietoj2+=5;}
            if(inforisif!=0){
                kuriojvietoj1+=7;
                kuriojvietoj2+=7;}
            try {
                pirmareiksme2 = (Integer.parseInt(splitif[kuriojvietoj1]));
            } catch (NumberFormatException e) {
                for (int l = 0; l < firstintkint.size(); l++) {
                    if(splitif[kuriojvietoj1].equals(firstintkint.get(l)))
                    {
                        int1=true;
                        pirmareiksme2=firstintvalue.get(l);
                    }
                }
            }
            try {
                antrareiksme2 = (Integer.parseInt(splitif[kuriojvietoj2]));
            }

            catch (NumberFormatException e) {
                for (int l = 0; l < firstintkint.size(); l++) {
                    if(splitif[kuriojvietoj2].equals(firstintkint.get(l)))
                    {
                        int2=true;
                        antrareiksme2=firstintvalue.get(l);
                    }
                }
            }
        }

        if (artesti&&isif==0) {

            if (( stringarchar&&int1==false)||(stringarchar&&int2==false)||(int1==false&&int2==false)) {
                if (pirmareiksme.equals(antrareiksme) && splitif[2].equals("=="))
                    vykdyk++;
                else if (!pirmareiksme.equals(antrareiksme) && splitif[2].equals("!="))
                    vykdyk++;
            }
            else if (pirmareiksme2 == antrareiksme2 && splitif[kuriojvietojvidurio].equals("==") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 != antrareiksme2 && splitif[kuriojvietojvidurio].equals("!=") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 < antrareiksme2 && splitif[kuriojvietojvidurio].equals("<") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 > antrareiksme2 && splitif[kuriojvietojvidurio].equals(">") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 <= antrareiksme2 && splitif[kuriojvietojvidurio].equals("<=") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 >= antrareiksme2 && splitif[kuriojvietojvidurio].equals(">=") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else {
                artesti = false;
                vykdyk = 0;
            }
        }
        else if (artesti&&isif==1) {
            if(inforisif==0){
                kuriojvietojvidurio+=5;}
            if(inforisif!=0){
                kuriojvietojvidurio+=7;}
            if (pirmareiksme2 == antrareiksme2 && splitif[kuriojvietojvidurio].equals("==") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 != antrareiksme2 && splitif[kuriojvietojvidurio].equals("!=") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 < antrareiksme2 && splitif[kuriojvietojvidurio].equals("<") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 > antrareiksme2 && splitif[kuriojvietojvidurio].equals(">") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 <= antrareiksme2 && splitif[kuriojvietojvidurio].equals("<=") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else if (pirmareiksme2 >= antrareiksme2 && splitif[kuriojvietojvidurio].equals(">=") && pirmareiksme2 + antrareiksme2 != 0) {
                vykdyk++;
            } else {
                artesti = false;
                vykdyk = 0;
            }

        }
        wasif=vykdyk;
        if (vykdyk==0){
            int vieta = 0;
            if (isif==1&&inforisif!=0) {
                vieta = 13;//10
            }
            if (isif==0&&inforisif!=0) {
                vieta = 6;//5
            }
            if (isif==1&&inforisif==0) {
                vieta = 10;
            }
            if (isif==0&&inforisif==0) {
                vieta = 5;
            }

            for (int l = 0; l < splitif.length; l++) {
                if(splitif[l].startsWith("else"))
                {
                    String viduje = splitif[l];
                    String[] ife = viduje.split("[{;}]");
                    String stringas=null;
                    for (int m = 0; m < ife.length; m++) {
                        stringas+="/"+ife[m]+"/";
                    }
                    input(stringas);
                }}}
        if ( vykdyk != 0 )
        {
            int vieta = 0;
            if (isif==1&&inforisif!=0) {
                vieta = 13;
            }
            if (isif==0&&inforisif!=0) {
                vieta = 6;
            }
            if (isif==1&&inforisif==0) {
                vieta = 10;
            }
            if (isif==0&&inforisif==0) {
                vieta = 5;
            }
            String viduje = splitif[vieta];
            String[] ife = viduje.split("[{;}]");
            int kiek = ife.length;
            for (int l = 0; l < ife.length; l++) {
                if (ife[l].equals("jump")) {
                    jump();
                } 
                if (ife[l].startsWith("while")) {
                    inifiswhile="while( "+splitif[6]+" "+splitif[7]+" "+splitif[8]+" "+splitif[9]+" "+splitif[10];//+" "+splitif[11]+" "+splitif[12];

                    recognizewhile(inifiswhile);

                }
                else if (ife[l].startsWith("for")) {

                    inifisfor="for(int "+splitif[6]+" "+splitif[7]+" "+splitif[8]+" "+splitif[9]+" "+splitif[10]+" "+splitif[11]+" "+splitif[12];
                    recognizefor(inifisfor);

                }
                if (ife[l].startsWith("if")||ife[l].startsWith(";if")) {
                    isif++;
                    ifassss.add(11 + l);
                    kiek = l;
                    String ifass = "";
                    String ifassa = "";

                    int ifvietapab=0;
                    for (int u = 0; u < ife.length; u++) {
                        if (ife[u].endsWith("}"))
                        {
                            ifvietapab=u;
                        }

                    }
                    for (int d = l+6; d < ifvietapab; d++) {
                        ifassa += " " + ife[d];

                    }

                    if(isif==1){
                        ifas(ifas,isif);}
                }
            }


        }
        isif=0;
        inforisif=0;
        wasif=vykdyk;
    }





    public static int isfor = 0;
    public void recognizefor(String zodis)
    {
        String foras = zodis;
        String splitfor[] = foras.split("[ (),]");
        int firstvalue1 = 0;
        int secondvalue1 = 0;
        int run = 0;
        boolean artesti=true;
        boolean int1 = false;
        boolean int2 = false;
        String firstvalue = null;
        String secondvalue = null;
        try {firstvalue1=(Integer.parseInt(splitfor[4])); 
        }
        catch(NumberFormatException e)
        {

            for (int a = 0; a < firstintkint.size(); a++) {
                if(splitfor[4].equals(firstintkint.get(a)))
                {
                    int1=true;
                    secondvalue1=firstintvalue.get(a);
                }
            }
        }
        try {secondvalue1=(Integer.parseInt(splitfor[8]));
        }
        catch(NumberFormatException e)
        {
            for (int a = 0; a < firstintkint.size(); a++) {
                if(splitfor[8].equals(firstintkint.get(a)))
                {
                    int2=true;
                    secondvalue1=firstintvalue.get(a);
                }
            }
        }
        int isif1 = 0; 
        int iselse = 0;  

        ArrayList<Integer> if1 = new ArrayList<Integer>(); 
        ArrayList<Integer> else1 = new ArrayList<Integer>();  
        ArrayList<Integer> for1 = new ArrayList<Integer>();       
        ArrayList<Integer> parentheses = new ArrayList<Integer>();  
        for (int a = 0; a <splitfor.length ; a++) {
            if(splitfor[a].endsWith("}")) {
                parentheses.add(a);
            }
        }
        String in = null;
        int where = 0;
        if(isfor == 0){in = splitfor[11]; //11
        }
        else{
            { for (int b = 0; b <splitfor.length ; b++) {
                if (splitfor[b].startsWith("{")&&splitfor[b-1].endsWith("++")) {
                    {in = splitfor[b];}
                }}}
        }
        {
            String[] infor = in.split("[{;}]");
            int count = infor.length;
            for (int h = 0; h < count; h++) {
                if (infor[h].equals("if")) {
                    isif1++;
                    if1.add(11 + h);
                    inforisif++;
                }
                if (infor[h].equals("else")) {
                    iselse++;
                    else1.add(11 + h);
                }
                if (infor[h].startsWith("for")) {
                    isfor++;
                    if(h!=0){
                        for1.add(11 + h);
                        kur=11+h;}
                    else {for1.add(11);
                        kur=11;}
                }
               if (infor[h].equals("jump")) {
                    for (int p = firstvalue1; p < secondvalue1; p++) {
                        jump();
                    }
                } }
                String inforiswhile = "while( ";
                if(infor[h].startsWith("while")) {
                    int where1 =0;
                    for (int k = 0; k < splitfor.length; k++) {
                        if(splitfor[k].contains("while")){
                            where1 = k;}
                    }
                    for (int k = where1+1; k < splitfor.length; k++) {
                        if(k==where1+1){inforiswhile+=splitfor[k];}
                        else if(k==where1+2){inforiswhile+=splitfor[k]+" ";}
                        else if(k==where+3){inforiswhile+=splitfor[k]+" ";}
                        else if(k == where1+4){inforiswhile+=splitfor[k]+" ";}
                        else if(k == where1+5){inforiswhile+=splitfor[k]+") ";}
                        else if(k == where1+6){inforiswhile+=splitfor[k];}
                        else{inforiswhile+=splitfor[k]+" ";}
                    }

                    for (int p = firstvalue1; p < secondvalue1; p++) {
                        recognizewhile(inforiswhile);
                    }
                }}
        }
        if(isif1!=0) {
            String if5 = "";
            for (int g = if1.get(0); g < splitfor.length; g++) {
                if5 += " "+splitfor[g];
            }
            for (int p = firstvalue1; p <secondvalue1; p++) {
                inforisif = 1;
                ifas(if5,0);
            }
            isif1--;
        }

        if(isfor!=0) {
            String forasss = "";
            for (int d = kur-1; d < splitfor.length; d++) {
                if(d!=-1){
                    forasss += " "+splitfor[d];
                }
            }
            for (int p = firstvalue1; p <secondvalue1; p++) {
                recognizefor(forasss);
            }
            isfor--;
        }
    }
    public void jump()
    {
        if(istheend){try {Thread.sleep(1*walks);} catch (InterruptedException e) {e.printStackTrace();}}
        else{try {Thread.sleep(50*walks);} catch (InterruptedException e) {e.printStackTrace();}}
        walks = 0;
        if(zaidejas.b2body.getLinearVelocity().y == 0){zaidejas.b2body.applyLinearImpulse(new Vector2(0, 4f), zaidejas.b2body.getWorldCenter(), true);}
        else if(zaidejas.b2body.getLinearVelocity().y != 0){
            try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
            zaidejas.b2body.applyLinearImpulse(new Vector2(0, 4f), zaidejas.b2body.getWorldCenter(), true);
        }
    }

    boolean istheend = false;
}

And my world loading class:

public class Main extends Game {
    public SpriteBatch spriteBatch;
    public static int Width = 400;
    public static int Height = 208;
    public static final short objectwhit = 32;
    public static final short mariowhit= 2;
    public static final short itemwhit = 256;
    public static float PPM = 100;
    public static final short marioheadwhit = 512;
    public static final short blockwhit = 4;
    public static final short coinwhit = 8;
    public static final short nothing = 0;
    public static final short ground = 1;
    public static final short destroyedwhit = 16;
    public static final short enemywhit = 64;
    public static final short enemyheadwhit = 128;
    public static inputs inp1;


    @Override
    public void create () {
        spriteBatch = new SpriteBatch();
        inp1 = new inputs(spriteBatch);
        setScreen(new MainCodeProgram(this,""));
    }


    @Override
    public void dispose() {
        super.dispose();
        spriteBatch.dispose();
    }


}

I hope you will help me with this mistake. Thank you

Regina
  • 55
  • 1
  • 2
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Kai Burjack May 12 '20 at 10:09
  • When you start a new LibGDX program it doesn't need JFrame, because it already has one. This tutorial will help you to arrange better the game and also with the texts. The same I included in your other question :-). Click [here](https://www.youtube.com/playlist?list=PLZm85UZQLd2SXQzsF-a0-pPF6IWDDdrXt "Youtube Tutorial") Youtube Tutorial. Click [here](https://github.com/BrentAureli/SuperMario "GitHub code") GitHub codes. – EL.ALEX78UK May 20 '20 at 09:29

0 Answers0