Problems with Nullpointerexception in a class

Asked

Viewed 37 times

-3

I have this code below that throws me the exception of NullPointerException. Besides, oddly enough he’s not "printing" anything,?

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;



public class SMAReader {

    public static void main(String[] args) throws IOException {

        URL oracle = new URL("https://www.alphavantage.co/query?function=SMA&symbol=MSFT&interval=30min&time_period=10&series_type=open&apikey=1DZO65QEO50KA0XG");
        URL close = new URL("https://www.alphavantage.co/query?function=SMA&symbol=MSFT&interval=30min&time_period=10&series_type=close&apikey=1DZO65QEO50KA0XG");
        int i;
        Double[] mediaDouble = new Double[10000];//cria o array que vai armazenar as medias em double
        Double[] mediaDoubleClose = new Double[10000];


        try (
            /*BufferedReader, lê o arquivo com mais eficiência 
            InputStreamReader, ele converte bytes p caracteres q seria o fluxo"Stream" q entra
            openStream(), abre o acesso do fluxo pela url, ou seja, ele conecta e recupera a informação da página   
            */
            BufferedReader in = new BufferedReader(
            new InputStreamReader(oracle.openStream()))) {
            String inputLine;//do buffered
            String[] media = new String[10000]; //cria o array que vai armazenar as medias
            String[] mediaClose = new String[10000];//cria o array que vai armazenar as medias do close
            int indiceMedia = 0;//indice do media
            int indiceClose = 0; //indice do close


            while ((inputLine = in.readLine()) != null) {//enquanto a linha nao estiver vazia
                if (inputLine.contains("SMA")) {//se a linha contiver sma

                    for (i = 0; i < inputLine.length(); i++ ) {//percorre ela
                        if (inputLine.charAt(i) == ':') { //se na linha tbm houver :
                            for (int j = i; j < inputLine.length()-1; j++) { //para j comecando no valor de i,j menor doq inputline-1
                                if (inputLine.substring(13, 16).equals("SMA")) { //se oq estiver nas posicoes de 13 a 16 corresponder a sema
                                    media[indiceMedia++] = inputLine.substring(20, inputLine.length()-1);//vai armazenar esse valor pro array
                                }
                            }
                            break;
                        }//fim if 2
                    }//fim for
                }//fim if1
            }//fim while


             while ((inputLine = in.readLine()) != null) {//enquanto houver linhas
                if (inputLine.contains("close")) {//se a linha contiver close
                    for (i = 0; i < inputLine.length(); i++ ) { //percorre o inputline ate -1 do tamanho dele
                        if (inputLine.charAt(i) == ':') { //se na linha houver :
                            for (int j = i; j < inputLine.length()-1; j++) {//para j = i,j menor doq o tamanho -1 do inputLine
                                if (inputLine.substring(13, 17).equals("close")) {//se oq estiver nas linhas 13 a 17 for igual a close
                                    mediaClose[indiceMedia++] = inputLine.substring(20, inputLine.length()-1);//cria um array que recebe esses valores
                                }//fim if
                            }//fim for
                            break;
                        }//fim if
                    }//fim for
                }//fim if
            }//fim while



            //cria o array que vai armazenar as medias do close em double tambem 
            for (i = 0; i < indiceMedia-1; i++) { //percorre o array
                for(int j = 0;j < indiceClose;j++) //percorre o array
                mediaDouble[i] = Double.parseDouble(media[i]); //e transforma em double
                mediaDoubleClose[i] = Double.parseDouble(mediaClose[i]);
            }//fim do for

            System.out.println("teste");
        } 
        System.out.println("teste"); 
    }
}
  • What is the error line? Post stacktrace to make it easy...

  • I don’t know why so much downvote. Sometimes when we’re at the beginning, learning, it’s hard to explain what the problem is. I agree that the question could - and should - be better formulated, but I think the purpose here is to help/teach.

1 answer

1

You’re getting a NullPointerException along those lines: mediaDoubleClose[i] = Double.parseDouble(mediaClose[i]);. It is not "printing" anything, because before arriving at the print call, the error is occurring.

Nullpointerexception happens when you try to access an object and that object is null. In this case, mediaClose[i] is void.

There are some ways to solve, but the easiest way to unlock this problem is to put a null check:

  if (mediaClose[i] != null) {
    mediaDoubleClose[i] = Double.parseDouble(mediaClose[i]);
  }

This way your program will run and you will see the output on the console.

More information (in English): https://en.wikibooks.org/wiki/Java_Programming/Preventing_NullPointerException

Browser other questions tagged

You are not signed in. Login or sign up in order to post.