Skip to content

Library import null check operator used on a null value #67

@tnc1997

Description

@tnc1997

No problem, thanks for the heads up. I've managed to reproduce the aforementioned issue by splitting up xml_serializable_example.dart into separate files:

xml_serializable_example.dart
import 'package:xml/xml.dart';

import 'bookshelf.dart';

void main() {
  final document = XmlDocument.parse(
    '''<?xml version="1.0"?>
    <bookshelf>
      <book>
        <title lang="English">XML Pocket Reference</title>
        <author>Simon St. Laurent</author>
        <author>Michael James Fitzgerald</author>
        <price></price>
      </book>
      <book>
        <title lang="English">HTML and XHTML Pocket Reference</title>
        <author>Jennifer Niederst Robbins</author>
        <price></price>
      </book>
    </bookshelf>''',
  );

  final bookshelf = Bookshelf.fromXmlElement(document.rootElement);
  print(bookshelf);

  final element = bookshelf.toXmlElement();
  print(element);
}
bookshelf.dart
import 'package:xml/xml.dart';
import 'package:xml_annotation/xml_annotation.dart' as annotation;

import 'book.dart';

part 'bookshelf.g.dart';

@annotation.XmlRootElement(name: 'bookshelf')
@annotation.XmlSerializable()
class Bookshelf {
  @annotation.XmlElement(name: 'book')
  List<Book>? books;

  @annotation.XmlElement(name: 'price', includeIfNull: false)
  String? price;

  Bookshelf({
    this.books,
    this.price,
  });

  factory Bookshelf.fromXmlElement(XmlElement element) =>
      _$BookshelfFromXmlElement(element);

  @override
  String toString() {
    return 'Bookshelf{books: $books, price: $price}';
  }

  void buildXmlChildren(
    XmlBuilder builder, {
    Map<String, String> namespaces = const {},
  }) =>
      _$BookshelfBuildXmlChildren(
        this,
        builder,
        namespaces: namespaces,
      );

  void buildXmlElement(
    XmlBuilder builder, {
    Map<String, String> namespaces = const {},
  }) =>
      _$BookshelfBuildXmlElement(
        this,
        builder,
        namespaces: namespaces,
      );

  List<XmlAttribute> toXmlAttributes({
    Map<String, String?> namespaces = const {},
  }) =>
      _$BookshelfToXmlAttributes(
        this,
        namespaces: namespaces,
      );

  List<XmlNode> toXmlChildren({
    Map<String, String?> namespaces = const {},
  }) =>
      _$BookshelfToXmlChildren(
        this,
        namespaces: namespaces,
      );

  XmlElement toXmlElement({
    Map<String, String?> namespaces = const {},
  }) =>
      _$BookshelfToXmlElement(
        this,
        namespaces: namespaces,
      );
}
book.dart
import 'package:xml/xml.dart';
import 'package:xml_annotation/xml_annotation.dart' as annotation;

import 'title.dart';

part 'book.g.dart';

@annotation.XmlRootElement(name: 'book')
@annotation.XmlSerializable()
class Book {
  @annotation.XmlElement(name: 'title')
  Title? title;

  @annotation.XmlElement(name: 'author')
  List<String>? authors;

  @annotation.XmlElement(name: 'price', isSelfClosing: false)
  String? price;

  Book({
    this.title,
    this.authors,
    this.price,
  });

  factory Book.fromXmlElement(XmlElement element) =>
      _$BookFromXmlElement(element);

  @override
  String toString() {
    return 'Book{title: $title, authors: $authors, price: $price}';
  }

  void buildXmlChildren(
    XmlBuilder builder, {
    Map<String, String> namespaces = const {},
  }) =>
      _$BookBuildXmlChildren(
        this,
        builder,
        namespaces: namespaces,
      );

  void buildXmlElement(
    XmlBuilder builder, {
    Map<String, String> namespaces = const {},
  }) =>
      _$BookBuildXmlElement(
        this,
        builder,
        namespaces: namespaces,
      );

  List<XmlAttribute> toXmlAttributes({
    Map<String, String?> namespaces = const {},
  }) =>
      _$BookToXmlAttributes(
        this,
        namespaces: namespaces,
      );

  List<XmlNode> toXmlChildren({
    Map<String, String?> namespaces = const {},
  }) =>
      _$BookToXmlChildren(
        this,
        namespaces: namespaces,
      );

  XmlElement toXmlElement({
    Map<String, String?> namespaces = const {},
  }) =>
      _$BookToXmlElement(
        this,
        namespaces: namespaces,
      );
}
title.dart
import 'package:xml/xml.dart';
import 'package:xml_annotation/xml_annotation.dart' as annotation;

import 'language.dart';

part 'title.g.dart';

@annotation.XmlRootElement(name: 'title')
@annotation.XmlSerializable()
class Title {
  @annotation.XmlAttribute(name: 'lang')
  Language? language;

  @annotation.XmlText()
  String? text;

  Title({
    this.language,
    this.text,
  });

  factory Title.fromXmlElement(XmlElement element) =>
      _$TitleFromXmlElement(element);

  @override
  String toString() {
    return 'Title{language: $language, text: $text}';
  }

  void buildXmlChildren(
    XmlBuilder builder, {
    Map<String, String> namespaces = const {},
  }) =>
      _$TitleBuildXmlChildren(
        this,
        builder,
        namespaces: namespaces,
      );

  void buildXmlElement(
    XmlBuilder builder, {
    Map<String, String> namespaces = const {},
  }) =>
      _$TitleBuildXmlElement(
        this,
        builder,
        namespaces: namespaces,
      );

  List<XmlAttribute> toXmlAttributes({
    Map<String, String?> namespaces = const {},
  }) =>
      _$TitleToXmlAttributes(
        this,
        namespaces: namespaces,
      );

  List<XmlNode> toXmlChildren({
    Map<String, String?> namespaces = const {},
  }) =>
      _$TitleToXmlChildren(
        this,
        namespaces: namespaces,
      );

  XmlElement toXmlElement({
    Map<String, String?> namespaces = const {},
  }) =>
      _$TitleToXmlElement(
        this,
        namespaces: namespaces,
      );
}
language.dart
import 'package:xml_annotation/xml_annotation.dart' as annotation;

part 'language.g.dart';

@annotation.XmlEnum(fieldRename: annotation.FieldRename.pascal)
enum Language {
  mandarin,
  spanish,
  english,
  hindi,
  bengali,
}
Output of dart run build_runner build
4s xml_serializable on 5 inputs: 1 no-op; spent 3s analyzing; example/bookshelf.dart
0s source_gen:combining_builder on 207 inputs
0s build_test:test_bootstrap on 119 inputs

example/book.dart xml_serializable
E Null check operator used on a null value
  #0      XmlSerializableGenerator._xmlSerializableSerializerGeneratorFactory (package:xml_serializable/src/xml_serializable_generator.dart:381:33)
  #1      XmlSerializableGenerator._generateBuildXmlChildren (package:xml_serializable/src/xml_serializable_generator.dart:124:48)
  #2      XmlSerializableGenerator.generateForAnnotatedElement (package:xml_serializable/src/xml_serializable_generator.dart:53:7)
  #3      GeneratorForAnnotation.generate (package:source_gen/src/generator_for_annotation.dart:93:30)
  #4      _generate (package:source_gen/src/builder.dart:370:33)
  <asynchronous suspension>
  #5      Stream.toList.<anonymous closure> (dart:async/stream.dart:1417:7)
  <asynchronous suspension>
  #6      _Builder._generateForLibrary (package:source_gen/src/builder.dart:116:9)
  <asynchronous suspension>
  #7      _Builder.build (package:source_gen/src/builder.dart:108:5)
  <asynchronous suspension>
  #8      runBuilder.buildForInput (package:build_runner/src/build/run_builder.dart:85:7)
  <asynchronous suspension>
  #9      Future.wait.<anonymous closure> (dart:async/future.dart:546:21)
  <asynchronous suspension>
  #10     BuildLogLogger.scopeLogAsync.<anonymous closure> (package:build_runner/src/logging/build_log_logger.dart:58:13)
  <asynchronous suspension>
  
example/bookshelf.dart xml_serializable
E Null check operator used on a null value
  #0      XmlSerializableGenerator._xmlSerializableSerializerGeneratorFactory (package:xml_serializable/src/xml_serializable_generator.dart:381:33)
  #1      XmlSerializableGenerator._xmlSerializableSerializerGeneratorFactory (package:xml_serializable/src/xml_serializable_generator.dart:402:11)
  #2      XmlSerializableGenerator._generateBuildXmlChildren (package:xml_serializable/src/xml_serializable_generator.dart:124:48)
  #3      XmlSerializableGenerator.generateForAnnotatedElement (package:xml_serializable/src/xml_serializable_generator.dart:53:7)
  #4      GeneratorForAnnotation.generate (package:source_gen/src/generator_for_annotation.dart:93:30)
  #5      _generate (package:source_gen/src/builder.dart:370:33)
  <asynchronous suspension>
  #6      Stream.toList.<anonymous closure> (dart:async/stream.dart:1417:7)
  <asynchronous suspension>
  #7      _Builder._generateForLibrary (package:source_gen/src/builder.dart:116:9)
  <asynchronous suspension>
  #8      _Builder.build (package:source_gen/src/builder.dart:108:5)
  <asynchronous suspension>
  #9      runBuilder.buildForInput (package:build_runner/src/build/run_builder.dart:85:7)
  <asynchronous suspension>
  #10     Future.wait.<anonymous closure> (dart:async/future.dart:546:21)
  <asynchronous suspension>
  #11     BuildLogLogger.scopeLogAsync.<anonymous closure> (package:build_runner/src/logging/build_log_logger.dart:58:13)
  <asynchronous suspension>
  
Log overflowed the console, switching to line-by-line logging.
  4s xml_serializable on 5 inputs: 1 skipped, 2 same, 2 no-op; spent 3s analyzing
  0s source_gen:combining_builder on 207 inputs; example/book.dart
  0s source_gen:combining_builder on 207 inputs: 205 skipped, 1 output, 1 same
  0s build_test:test_bootstrap on 119 inputs; $package$
  0s build_test:test_bootstrap on 119 inputs: 119 skipped
  Failed to build with build_runner/jit in 6s; wrote 4 outputs.

The error is on this line, the value of prefix is null when an import doesn't have prefix (see documentation). This change was added in 0f6b3e7.

Originally posted by @nvi9 in #66

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions