bivashy / java-vk-bots-long-poll-api

A Java library to create VK bots using Bots Long Poll API
MIT License
3 stars 0 forks source link

Cannot invoke "com.google.gson.JsonObject.has(String)" because "jsonObject" is null #33

Closed LikeKey1 closed 3 years ago

LikeKey1 commented 3 years ago

Ошибка:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.google.gson.JsonObject.has(String)" because "jsonObject" is null at api.longpoll.bots.utils.converters.AttachmentConverterUtil.extractAttachments(AttachmentConverterUtil.java:15) at api.longpoll.bots.converters.basic.MessageConverter.convert(MessageConverter.java:16) at api.longpoll.bots.converters.basic.MessageConverter.convert(MessageConverter.java:12) at api.longpoll.bots.converters.events.messages.MessageNewEventConverter.convert(MessageNewEventConverter.java:16) at api.longpoll.bots.converters.events.messages.MessageNewEventConverter.convert(MessageNewEventConverter.java:10) at api.longpoll.bots.converters.events.EventConverter.convert(EventConverter.java:56) at api.longpoll.bots.converters.events.EventConverter.convert(EventConverter.java:47) at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) at java.base/java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:1003) at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) at api.longpoll.bots.utils.converters.BulkConverterUtil.convert(BulkConverterUtil.java:16) at api.longpoll.bots.converters.response.events.GetEventsResultConverter.convert(GetEventsResultConverter.java:21) at api.longpoll.bots.converters.response.events.GetEventsResultConverter.convert(GetEventsResultConverter.java:13) at api.longpoll.bots.methods.Method.execute(Method.java:72) at api.longpoll.bots.methods.Method.execute(Method.java:55) at api.longpoll.bots.server.LongPollServer.getUpdates(LongPollServer.java:43) at api.longpoll.bots.BotsLongPoll.run(BotsLongPoll.java:25) at ru.likekey.vkbot.Application.main(Application.java:17)

Как выглядят классы: ` public class MyBot extends LongPollBot {

@Override
public void onMessageNew(MessageNewEvent messageNewEvent) {
    Message message = messageNewEvent.getMessage();
    if (message.hasText()) {
        String response = "Hello! Received your message: " + message.getText();
        try {
            new MessagesSend(this)
                    .setPeerId(message.getPeerId())
                    .setMessage(response)
                    .execute();
        } catch (BotsLongPollHttpException e) {
            e.printStackTrace();
        } catch (BotsLongPollException e) {
            e.printStackTrace();
        }
    }
}

@Override
public String getAccessToken() {
    return "токен";
}

@Override
public int getGroupId() {
    return ид;
}

} `

` public class Application {

public static void main(String[] args) throws ClientException, ApiException, IOException, BotsLongPollHttpException, BotsLongPollException {
    new BotsLongPoll(new MyBot()).run();
}

} `

yvasyliev commented 3 years ago

Можете поделится примером JSON, который вы получаете?

Чтоб получить JSON нужно:

  1. Добавить в pom.xml:
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.30</version>
    </dependency>
  2. Установить уровень логирования DEBUG в методе main:
    public static void main(String[] args) {
    System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
    new BotsLongPoll(new MyBot()).run();
    }

    Этот JSON должен содержать поле "type": "message_new" Пример такого JSON: https://github.com/yvasyliev/java-vk-bots-long-poll-api/blob/master/src/test/resource/json/response/message_new/message_new_photo_sample_5_110.json

LikeKey1 commented 3 years ago

Теперь к началу предыдущей ошибки добавилось это: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/C:/Users/aleha/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/C:/Users/aleha/.m2/repository/org/slf4j/slf4j-simple/1.7.30/slf4j-simple-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2

yvasyliev commented 3 years ago

Похоже, что у вас в проекте уже подключена log4j 2 имплементация.

В таком случае:

  1. Уберите
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.30</version>
    </dependency>
  2. Поместите в папку resource файл log4j2.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
    </Configuration>
  3. Запустите приложение:
    public static void main(String[] args) {
    new BotsLongPoll(new MyBot()).run();
    }
yvasyliev commented 3 years ago

Пожалуйста, сообщите, если у вас снова воспроизведётся эта ошибка.

yvasyliev commented 3 years ago

Дайте знать, если это ещё актуально.