1

I am trying to load an xml file using scala-xml_2.12-1.0.6.jar but it gives me NullPointerEexception while loading

Following is my line of code to load xml

import scala.xml.XML
val xml = XML.loadFile("sample.xml")

I have decompiled this jar and is method is present in that jar but for some reasons it is unable to find it in code.

I have Scala 2.13.1 on my system but for this project I am using scala 2.12.1 and it is mentioned in mu built.sbt

scalaVersion := "2.12.1"

I have following dependency in my built.sbt for this xml package

libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.0.6"

If I copy and paste the same code to Scala interactive shell( scala 2.13.1) I get following error

import scala.xml.XML
                ^
   error: object xml is not a member of package scala
   did you mean Nil?

Can anyone please identify what am i doing wrong?

Thanks in advance.

M.Ahsen Taqi
  • 965
  • 11
  • 35
  • We need more info to help you. Have you tried adding this to build.sbt? – Iva Kam Apr 01 '21 at 08:38
  • @IvaKam thanks for replying me, I have the library dependency for this xml package with which I am facing this issue . I have added that in my question, please have a look. Thanks – M.Ahsen Taqi Apr 01 '21 at 13:12

1 Answers1

1

I'm not sure how you are loading up the Scala REPL, but as mentioned in "How to use third party libraries with Scala REPL?", you should be launching the the REPL from SBT with sbt console. Your .sbt files will also need to be in scope.

The Scala REPL independently does not deal with .sbt files. They are 2 different tools.

Alternatively you could also install Ammonite-REPL which supports Magic Imports. You will be able to import Maven dependencies with import $ivy.

Scala 2.12 comes with scala-xml and you can remove that dependency as long as you run REPL with sbt console and not your native Scala REPL which is already @ Scala 2.13. Otherwise you can also switch to Scala 2.13 for your SBT project.

yangzai
  • 962
  • 5
  • 11