vendredi 2 décembre 2016

how to insert a null check for getMethod (java)

My code is calling an API correctly but after I made an encryption for the link I started to receive this error "java.lang.IllegalArgumentException: host parameter is null" I know I have to do a null check but I am not sure how and where. Would someone help

public class ReadData
{
    public static void main(String[] args)
    {
        final File data = new File("testData.txt");
        final File quads = new File("quads.txt");

        try (final FileInputStream fi = new FileInputStream(data);
             final Scanner scanner = new Scanner(fi);
             final FileWriter fw = new FileWriter(quads);
             final BufferedWriter writer = new BufferedWriter(fw))
        {
            while (scanner.hasNextLine())
            {
                final String line = scanner.nextLine();
                final String[] triple = line.split(" ");

                final String subj = triple[0].substring(1, triple[0].length() - 1);
                final String obj = triple[2].substring(1, triple[2].length() - 1);

                maxWeight = 0.0;
                final double subjWeight = calculateWeight(subj, obj);
                final double objWeight = calculateWeight(obj, subj);

                final double similarity = Math.max(subjWeight, objWeight);

                writer.write(triple[0]);
                writer.write(" ");
                writer.write(triple[1]);
                writer.write(" ");
                writer.write(triple[2]);
                writer.write(" ");
                writer.write(String.valueOf(similarity));
                writer.write(" .");
                writer.newLine();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    static private final HttpClient client = new HttpClient();
    static private Double maxWeight = 0.0;
    static private Integer maxIntegration = 5;

    static private double calculateWeight(String src, String target)
    {
        accumulateWeight(src, target, 0.0, maxIntegration);
        return maxWeight;
    }

    static private void accumulateWeight(String key, String target, Double accWeight, int integration)
    {
        if (key.equals(target))
        {
            final Double averageWeight = accWeight / (maxIntegration - integration);

            if (averageWeight > maxWeight)
            {
                maxWeight = averageWeight;
            }

            return;
        }

        if (integration == 0)
        {
            if (key.equals(target))
            {
                final Double averageWeight = accWeight / (maxIntegration - integration);

                if (averageWeight > maxWeight)
                {
                    maxWeight = averageWeight;
                }

                return;
            }

            return;
        }

        try {

            final GetMethod getMethod = new GetMethod(URLEncoder.encode("http://ift.tt/2gQyUMn" + key, "ISO-8859-1"));

            try
            {
                client.executeMethod(getMethod);

                final String jsonStr = getMethod.getResponseBodyAsString();
                final JsonObject root = new Gson().fromJson(jsonStr, JsonObject.class);

                for (JsonElement item : root.getAsJsonArray("edges"))
                {
                    final JsonObject entity = item.getAsJsonObject();
                    final String[] start = entity.getAsJsonPrimitive("start").getAsString().substring(1).split("\\/");

                    if (start[1].equals("en") && start[2].equals(key))
                    {
                        final String[] objs = entity.getAsJsonPrimitive("end").getAsString().substring(1).split("\\/");

                        if (objs[1].equals("en"))
                        {
                            final Double weight = entity.getAsJsonPrimitive("weight").getAsDouble();

                            accumulateWeight(objs[2].replace("\\", ""), target, accWeight + weight, integration - 1);
                        }
                    }
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire